javascript - Fetch selected item of DropDownList using $(this) object -


how fetch selected item of dropdownlist using $(this) ?

i have 2 dropdownlists in web page. want selected item name. tried 3 ways , each gave different result.

  1. this method showed selected-item in "1st list" , selected-item of "2nd list". guess because selector not qualified id.

  2. this method gave proper result. can achieve same result using $(this), instead of id. guess object pointing html-element.

  3. this method gave no results

    $(document).ready( function() { $('#idservertype').bind("change", loadx); } );

.

function loadx() {     var str = "";      /////  1     str = $("select option:selected").text();      console.log('menu clicked: ' + str);      /////  2     str = $("#idservertype option:selected").text();     console.log('menu clicked: ' + str);      /////  3     str = $("this option:selected").text(); //3     console.log('menu clicked: ' + str); } 

please note want keep event registration , event-handler separate; helps in code maintenance.

you can use $(this) children() selected option element.

str = $(this).children("option:selected").text(); 

children('option:selected') select selected option select element.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -