...

View Full Version : how do i pull what is in between the option tag



emehrkay
06-02-2006, 05:15 PM
in a dropdown box, i know that i can do ele.value to get the value of the option, but i want the label or 'xxx'

<option val='val'>xxx</option>

thanks

dumpfi
06-02-2006, 05:26 PM
Use ele.text.
dumpfi

emehrkay
06-02-2006, 05:31 PM
that returns undefined

document.getElementById('course_id').text //course_id is the dropdown

coothead
06-02-2006, 05:31 PM
Hi there emehrkay,

here is an example...

<script type="text/javascript">
<!--
window.onload=function(){
df=document.forms[0][0];
for(c=0;c<df.options.length;c++) {
alert(df[c].firstChild.data);
}
}
//-->
</script>

<form action="#">
<div>
<select>
<option>---</option>
<option>xxx</option>
<option>yyy</option>
<option>zzz</option>
</select>
</div>
</form>
coothead

emehrkay
06-02-2006, 05:48 PM
coothead that works, but how can i just pull the value that is selected?

coothead
06-02-2006, 06:02 PM
Hi there emehrkay,

no problem...

<script type="text/javascript">
<!--
window.onload=function(){
df=document.forms[0][0];
df.onchange=function() {
alert(df.options[df.options.selectedIndex].firstChild.data);
}
}
//-->
</script>

<form action="#">
<div>
<select>
<option value="0">---</option>
<option value="1">xxx</option>
<option value="2">yyy</option>
<option value="3">zzz</option>
</select>
</div>
</form>
coothead

emehrkay
06-02-2006, 06:06 PM
thanks coothead, i got it beforre i checked the thread

document.getElementById('course_id').childNodes[0].firstChild.nodeValue;

dumpfi
06-02-2006, 09:23 PM
document.getElementById('course_id').text //course_id is the dropdownI should have mentioned that this is a property of the option element and not the select element.

So you would have to use something like:

var sel = document.getElementById('course_id');
alert(sel.options[sel.options.selectedIndex].text);

dumpfi



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum