html - javascript function not getting dropdown text - Stack Overflow

i am using javascript to get the text of selected item from dropdown list.but i am not getting the tex

i am using javascript to get the text of selected item from dropdown list. but i am not getting the text. i am traversing the dropdown list by name..

my html dropdownlist is as:

<select name="SomeName" onchange="div1();">
    <option value="someVal">A</option>
    <option value="someOtherVal">B</option>
    <option value="someThirdVal">C</option>
</select>

and my javascript is as:

function div1() {     
     var select = document.getElementsByName("SomeName");     
     var result = select.options[select.selectedIndex].text;
     alert(result);
 }

can you please help me out..

i am using javascript to get the text of selected item from dropdown list. but i am not getting the text. i am traversing the dropdown list by name..

my html dropdownlist is as:

<select name="SomeName" onchange="div1();">
    <option value="someVal">A</option>
    <option value="someOtherVal">B</option>
    <option value="someThirdVal">C</option>
</select>

and my javascript is as:

function div1() {     
     var select = document.getElementsByName("SomeName");     
     var result = select.options[select.selectedIndex].text;
     alert(result);
 }

can you please help me out..

Share Improve this question edited May 27, 2011 at 10:27 Chris Harrison 5,8585 gold badges31 silver badges36 bronze badges asked May 27, 2011 at 10:06 SA.SA. 7527 gold badges21 silver badges40 bronze badges 1
  • 1 why don't you give an id to dropdown and use document.getElementById ? – Jayantha Lal Sirisena Commented May 27, 2011 at 10:09
Add a ment  | 

3 Answers 3

Reset to default 3

Option 1 - If you're just looking for the value of the selected item, pass it.

<select name="SomeName" onchange="div1(this.value);">
    <option value="someVal">A</option>
    <option value="someOtherVal">B</option>
    <option value="someThirdVal">C</option>
</select>

function div1(val)
{
    alert(val);
}

Option 2 - You could also use the ID as suggested.

<select id="someID" name="SomeName" onchange="div1();">
    <option value="someVal">A</option>
    <option value="someOtherVal">B</option>
    <option value="someThirdVal">C</option>
</select>

function div1()
{
    var ddl = document.getElementById("someID");
    var selectedText = ddl.options[ddl.selectedIndex].value;

    alert(selectedText);
}

Option 3 - You could also pass the object itself...

<select name="SomeName" onchange="div1(this);">
    <option value="someVal">A</option>
    <option value="someOtherVal">B</option>
    <option value="someThirdVal">C</option>
</select>

function div1(obj)
{
    alert(obj.options[obj.selectedIndex].value);
}

getElementsByName returns an array of items, so you'd need:

var select = document.getElementsByName("SomeName");      
var text = select[0].options[select[0].selectedIndex].text;     
alert(text);

Or something along those lines.

Edit: instead of the "[0]" bit of code, you probably want either (a) to loop all items in the "select" if you expect many selects with that name, or (b) give the select an id and use document.getElementById() which returns just 1 item.

The problem with the original snippet posted is that document.getElementsByName() returns an array and not a single element.

To fix the original snippet, instead of:

document.getElementsByName("SomeName");   // returns an array

try:

document.getElementsByName("SomeName")[0];  // returns first element in array

EDIT: While that will get you up and running, please note the other great alternative answers here that avoid getElementsByName().

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745308272a4621845.html

相关推荐

  • html - javascript function not getting dropdown text - Stack Overflow

    i am using javascript to get the text of selected item from dropdown list.but i am not getting the tex

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信