开发者

How to access a form drop down from Javascript

开发者 https://www.devze.com 2022-12-12 20:41 出处:网络
Is there a way to access a drop down options text using JavaScript? For instance I can access the value by doing:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消