Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple select problem

Status
Not open for further replies.

DotNetBlocks

Programmer
Apr 29, 2004
161
US
Hello,
I am still a beginner a javascript. What event do i use if a user selects more than one item in a dropdown list or combo box? I tried onchange and it did not work for the multiple select, but it does work for the single select. I am using IE on XP Pro.

Thanks

Babloome

 
Well, I thought that the answer would be that the multiple select box must first lose focus before the event would trigger. This is the case with textboxes, so I figured it would be the answer to your problem. However, that is not the case. Copy/Paste this into a new html file:
Code:
<select onchange="alert('changed the single select')">
   <option>1</option>
   <option>2</option>
   <option>3</option>
</select>

<br /><br />

<input type="text" onchange="alert('changed the textbox')" />

<br /><br />

<select multiple="multiple" size="3" onchange="alert('changed the multiple select')">
   <option>1</option>
   <option>2</option>
   <option>3</option>
</select>

You will see that in the single select, the alert box appears every time the option is changed. In the textbox, the alert box appears every time you change the content and the textbox loses focus. The multiple select box will trigger the onchange event every single time a new option is clicked in the box. It almost acts the same as you'd expect an onclick handler to behave.

I don't know if that answers your question, but it does prove that onchange works for multiple select boxes.

(I tried my example in ie7 and ff2)

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Thanks Kaht,
I am trying to get the values of all the selected items.
When on a multiselect combo box if i select more than one, the event does not fire.

Babloome

BTW, Nice pupies. :)







 
[tt]<select onchange="alert(this.value)">
<option value="x">1</option>
<option value="y">2</option>
<option value="z">3</option>
</select>

<br /><br />

<select multiple="multiple" size="3" onchange="var s=''; for (var i=0;i<this.length ;i++){if (this.options.selected) {s+=this.options.value+'\n'}}; alert(s)">
<option value="x">1</option>
<option value="y">2</option>
<option value="z">3</option>
</select>
[/tt]
 
BTW, Nice pupies. :)

Thanks [smile]

Just this weekend the mother is back in her cycle ready to have some more, so we've got a chore for the next month of keeping her away from the dad..... We're just not ready to mess with selling more puppies already.... [lol]

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top