By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

Select/Unselect items in multiple select using javascript

P: 1
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
Share this Question
Share on Google+
1 Reply


RamananKalirajan
100+
P: 607
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.