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.
this method showed selected-item in "1st list" , selected-item of "2nd list". guess because selector not qualified id.
this method gave proper result. can achieve same result using $(this), instead of id. guess object pointing html-element.
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
Post a Comment