PDA

View Full Version : help regarding onclick event


sachin lad
02-17-2005, 07:43 AM
hello,

I am newbie to java script can anyone help me with this..i have select control as ..

<select name="element" >
<option value =id1> name1</option>
<option value =id2> name2</option>
<option value =id3> name3</option>
</select>

what i want is to retrieve the name1,name2,name3..when corresponding id1,id2,id3.. is selected. I require both id/name pair. i was thinking to have a "hidden" type input, which i will change using onchange() event to reflect value of name.

Is there anyway to read "name1/name2/name3" whichever is selected? please reply..

Thanking you in advance
sachin

Kor
02-17-2005, 08:30 AM
The event you are looking for is onchange
The attributes you are looking for are: text and value.
value is select's attribute, while text is option's attribute
options represent a collection, thus you need a indexed loop to refere one of them using selectedIndex attribute
Now:

function alertVal(s){
var sVal = s.value;
var oTxt = s.options[s.selectedIndex].text;
if(s.selectedIndex!=0){
alert('Selected text is: '+oTxt+' and the selected value is: '+sVal)
}
}

...

...
<select name="element" onchange="alertVal(this)">
<option value =id1> name1</option>
<option value =id2> name2</option>
<option value =id3> name3</option>
</select>

sachin lad
02-17-2005, 09:34 PM
Thanks Kor,

It did worked..thanks a lot!

sachin

Kor
02-18-2005, 08:11 AM
You're welcome!