469,271 Members | 1,433 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Select/Unselect items in multiple select using javascript

Hi all,

I have a HTML multiple select (ListBox) control:

[HTML]<select id="mySelect" multiple>
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>[/HTML]

I'm looking for a way to select / unselect a few items in this list using javascript.
I saw, that selectedItem member allows to controll such functionality, but it allows to deal with a single item only, while I need multi-selections.

Any ideas?


Thanks in advance,
Michael.
Jul 29 '08 #1
1 9335
RamananKalirajan
607 512MB
Hi Michael, I hope this code will help you out

[HTML]<html>
<head>
<script type="text/javascript">
function selectMultiple()
{
for (var i=0; i<document.getElementById('mySelect').options.leng th; i++)
{
if (document.getElementById('mySelect').options[i].selected == true)
{
alert(document.getElementById('mySelect').options[i].text);
}
}
}
</script>
</head>
<body><form>
<select name="mySelect" size="10" multiple>
<option>Apple</option>
<option>Pear</option>
<option>Pea</option>
<option>Banana</option>
<option>Orange</option>
<option>Mango</option>
<option>Lime</option>
<option>Goa</option>
<option>Dates</option>
</select>
<input type="button" onclick="selectMultiple()"
value="Select multiple">
</form></body>
</html> [/HTML]

Regards
Ramanan Kalirajan
Jul 29 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by headware | last post: by
1 post views Thread by mike robinson | last post: by
6 posts views Thread by Ben Hallert | last post: by
2 posts views Thread by Chris Plowman | last post: by
1 post views Thread by sab | last post: by
4 posts views Thread by Matt Ratliff | last post: by
2 posts views Thread by Andy Baker | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.