views:

57

answers:

2

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 between 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>
+3  A: 

try

document.getElementById("transFrom").options[document.getElementById("transFrom").selectedIndex].text
Rob
That works. Thanks!!!
Josh Curren
A: 

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.

mschmidt42