How to access a form drop down from Javascript
Is there a way to access a drop down options text using JavaScript?
For instance I can access the value by doing:
document.getElementById("transFrom").value;
But I want the text betwee开发者_运维问答n the option tags.
Here is the HTML of a form drop down:
<select name="transFrom" id="transFrom" style="width: 300px;" tabindex="1" onfocus="return validate_field(this)" onchange="return validate_field(this)">
<option value="">Select An Account</option>
<option value="S">Savings</option>
<option value="C">Checking</option>
<option value="M">Money Market</option>
</select>
try
document.getElementById("transFrom").options[document.getElementById("transFrom").selectedIndex].text
If you are interested, you could use jQuery:
$('#transFrom').val();
http://docs.jquery.com/Attributes/val
I (along with thousands of others) have found jQuery to be extremely useful for many simple and complex javascript calls/functions. It's a fairly small include file for the amount of benefit you get.
精彩评论