javascript tutorial - [Solved-5 Solutions] jQuery get specific option tag text - javascript - java script - javascript array
Problem:
What would the selector look like if we wanted to get "Option B" when we have the value '2' ?
Solution 1:
$("#list[value='2']").text();
click below button to copy the code. By JavaScript tutorial team
Solution 2:
an element with id list which has a property value equal to 2. What we want is the option child of the list.
$("#list option[value='2']").text()
click below button to copy the code. By JavaScript tutorial team
Solution 3:
get the option with a value of 2, use
$("#list option[value='2']").text();
click below button to copy the code. By JavaScript tutorial team
Solution 4:
$("#list").change(function() {
alert($(this).find("option:selected").text()+' clicked!');
});
click below button to copy the code. By JavaScript tutorial team
It has been tested to work on Internet Explorer and Firefox.
Solution 5:
function selected_state(){
jQuery("#list option").each(function(){
if(jQuery(this).val() == "2"){
jQuery(this).attr("selected","selected");
return false;
}
});
}
jQuery(document).ready(function(){
selected_state();
});