jquery - How to get the text and value from a dropdown in Javascript -
i have basic question. want select selected text , dropdown value dropdown , show in alert box.
my attempt:
dropdown
<p id="test"> select draft: <select id="select" name="select"> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </select> </p>
js
$("#select").change(function() { alert($(this).val()); // if works fine next line code should work alert($(this).text()); // why shows dropdown texts alert($("#select option:selected").text()); // works fine });
question 1: $(this) signifies? if it's signifies selected element should show text when doing $(this).text()
. doesn't work expected.
question 2: if need select value , text of dropdown above mentioned efficient way go it.
please guide me.
in change
event handler, $(this)
<select>
element.
the element represented $(this)
depends on context used.
element triggered event (like here), or element targeted iteration of .each()
loop, example.
when <option>
selected, <select>
take selected option value value...
doesn't text of selected option.
so why second alert()
statement doesn't work.
Comments
Post a Comment