467,136 Members | 1,252 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,136 developers. It's quick & easy.

Processing delimited list then adding to listbox in java script

I haven't worked with java script much, I know how to do this server side by
constant post backs to the server on a webform, but it seems like i should
be able to do this client side also...

What happens is a modal dialog recieves a listing of users in a semi-colon
delimited list so its like this

user_1;user_2;user3;user_4;

what I need to do is split this up in javascript and place each user into a
listbox on the client side, how would i go about doing that? thanks
Nov 18 '05 #1
  • viewed: 1413
Share:
1 Reply
Hi Brian,

From your description, you'd like to split a string into an array in
javascript code and dynamically create some items of
a html listbox according to the values in the array via javascript , yes?

As for this problem, here are my suggestions:
1. The string spliting can be done by the split() member function of the
text object, for example

var txt = "user1;user2;user3";
var array = txt.split(";");

2. And as for dynamically create option items for listbox( infact it is a
<select> html element), we can use the following code:

//modelSelect is an <select > html element

modelSelect.options.length = 0; // Clear the popup

modelSelect.options[0] = new Option("Explorer");
modelSelect.options[1] = new Option("Mustang");
modelSelect.options[2] = new Option("Probe");

And here is a web tech article discussing this:
#Focus on JavaScript --- Modifying Items in a Dropdown List
http://javascript.about.com/library/.../aa072903a.htm

In addition, here a simple demo page , you may also have a look if you feel
anything unclear:
====================================
<HTML>
<HEAD>
<title>jslistbox</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function setUsers()
{
var txt = document.getElementById("txtUsers").value;
var lst = document.getElementById("lstUsers");

if(txt != null && txt.length != 0)
{
var arr_users = txt.split(";");
lst.options.length = 0;

var i=0;
for(i=0;i<arr_users.length;i++)
{
lst.options[i] = new Option(arr_users[i]);
}
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<SELECT size="5" id="lstUsers">
<OPTION></OPTION>
</SELECT>
</td>
</tr>
<tr>
<td><INPUT type="text" id="txtUsers"></td>
</tr>
<tr>
<td><INPUT id="btnSetUsers" type="button" value="Set Users"
onclick="setUsers()"></td>
</tr>
</table>
</form>
</body>
</HTML>
=============================================

Hope these help. Thanks.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Garry | last post: by
1 post views Thread by Anthony Liu | last post: by
3 posts views Thread by Brian Henry | last post: by
6 posts views Thread by hardik | last post: by
2 posts views Thread by ManningFan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.