Tuesday, July 31, 2012

jquery – Getting selected option text on change



I am trying to do something very simple but it\’s not working for me. All I want to do is get the selected option\’s text when the selected option is changed, something like this:



<select id = \"myselect\">
<option value = \"1\">One</option>
<option value = \"2\">Two</option>
<option value = \"3\">Three</option>
</select>


Then in JavaScript:



$(\"#myselect\").change(function () {
var text = $(this).find(\"option[selected]\").html();
alert(text);
});


Simple right? The problem is I am just getting null. When I inspect the DOM I find that changing the selected option does not change the \”selected\” attribute of any of the options. Doesn\’t changing the option typically update the DOM? I\’m pretty sure I\’ve done this before. What I am missing here? I am using FireFox ver 13.0.1 for my testing.






Rating: 4 out of 5 based on 5 ratings



The post jquery – Getting selected option text on change appeared first on Javascript ASK.






via Javascript ASK http://javascriptask.phpfogapp.com/jquery-getting-selected-option-text-on-change.html

No comments:

Post a Comment