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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Index 3

Status
Not open for further replies.

Mighty

Programmer
Feb 22, 2001
1,682
US
I have searched through the forum for the answer to my problem but i am pissed off and tired and I can't find the answer. Can someone please answer my question.

I have a select box and using the onchange event I want to pass the index of the selected item to a function but I can't do it.

I have tried:

onChange="return test(this.index)"
onChange="return test(this.selected.index)"
onChange="return test(this.options.selected.index)"

Someone please tell me the right way to do this. The first one above gives me "undefined" while the other two just give errors. Mise Le Meas,

Mighty :)
 
Do you want to pass the value of the item, or the item's array number in the select list? - tleish
 
[tt][COLOR=666666]<!-- Selected Index -->[/color][/tt]
[tt][COLOR=000080][tt][COLOR=FA5000]<SELECT NAME=&quot;mySelect&quot; ONCHANGE=&quot;return test(this.selectedIndex)&quot;>[/color][/tt][/color][/tt]

[tt][COLOR=666666]<!-- Selected Value -->[/color][/tt]
[tt][COLOR=000080][tt][COLOR=FA5000]<SELECT NAME=&quot;mySelect&quot; ONCHANGE=&quot;return test(this.value)&quot;>[/color][/tt][/color][/tt] - tleish
 
oops.. there is a typo in my previous post..

the correct one should be
onChange=&quot;test(this.selectedIndex)&quot;

Chiu Chan
WebMaster & Software Engineer
emagine solutions, inc
cchan@emagine-solutions.com
 
this.value doesn't work with a select list, you have to use this[this.selectedIndex].value Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Thanks Guys. Mise Le Meas,

Mighty :)
 
tsdragon, I tested the code before I posted it and this.value does work with Select lists. - tleish
 
Hi,

The &quot;this.value&quot; is only works for IE, Netscape will only give you &quot;null&quot;. So Tsdragoon is right, the following example will work for both broswers :)

<select onchange=&quot;alert(this.options[selectedIndex].value)&quot;>
<option value=&quot;1&quot;>1</option>
<option value=&quot;2&quot;>2</option>
<option value=&quot;3&quot;>3</option>
</select>

hope this helps, Chiu Chan
WebMaster & Software Engineer
emagine solutions, inc
cchan@emagine-solutions.com
 
Oops, didn't test in NS. Thx for the clearification - tleish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top