Option A Option B Option CWhat would the selector look like if I wanted khổng lồ get "Option B" when I have the value "2"?
Please note that this is not asking how khổng lồ get the selected text value, but just any one of them, whether selected or not, depending on the value attribute. I tried:
$("#list Bạn đang xem: Jquery get specific option tag text
Bạn đang xem: Jquery get specific option tag text
Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
Help us improve our answers.
Are the answers below sorted in a way that puts the best answer at or near the top?
It"s looking for an element with id danh mục which has a property value equal lớn 2.What you want is the option child of the list:
If you"d lượt thích to get the option with a value of 2, use
This worked perfectly for me, I was looking for a way lớn send two different values with options generated by MySQL, & the following is generic & dynamic:
As mentioned in one of the comments. With this I was able to create a dynamic function that works with all my selection boxes that I want to lớn get both values, the option value and the text.
Few days ago I noticed that when updating the jQuery from 1.6 to lớn 1.9 of the site I used this code, this stop working... Probably was a conflict with another piece of code... Anyway, the solution was to remove option from the find() call:
$(this).find(":selected").text();That was my solution... Use it only if you have any problem after updating your jQuery.
You can get selected option text by using function .text();
you can điện thoại tư vấn the function lượt thích this :
Based on the original HTML posted by Paolo I came up with the following.
$("#list").change(function() alert($(this).find("option:selected").text()+" clicked!"););It has been tested to work on internet Explorer và Firefox.
If there is only one select tag in on the page then you can specify select inside of id "list"
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(););
$("#list option:selected").each(function() alert($(this).text());); for multiple selected value in the #list element.
Try the following:
A tip: you can use below code if your value is dynamic:
$("#list option").filter(function() return this.value === aDynamicValue;).text();As mentioned in jQuery get specific option tag text & placing dynamic variable to lớn the value
You can get one of following ways
This is an old Question which has not been updated in some time the correct way to vì this now would be to use
$("#action").on("change",function() alert($(this).find("option:selected").text()+" clicked!"););I hope this helps :-)
I needed this answer as I was dealing with a dynamically cast object, và the other methods here did not seem to lớn work:
While "looping" through dynamically created select elements with a .each(function()...): $("option:selected").text(); and $(this + " option:selected").text() did not return the selected option text - instead it was null.
But Peter Mortensen"s solution worked:
$(this).find("option:selected").text();I vị not know why the usual way does not succeed in a .each() (probably my own mistake), but thank you, Peter. I know that wasn"t the original question, but am mentioning it "for newbies coming through Google."
I would have started with $("#list option:selected").each() except I needed to lớn grab stuff from the select element as well.
I was looking for getting val by internal field name instead of ID and came from google to lớn this post which help but did not find the solution I need, but I got the solution & here it is:
So this might help somebody looking for selected value with field internal name instead of using long id for SharePoint lists:
var e = $("select
As an alternative solution, you can also use a context part of jQuery selector lớn find element(s) with value="2" inside the dropdown list:
I wanted a dynamic version for select multiple that would display what is selected khổng lồ the right (wish I"d read on và seen $(this).find... Earlier):
opt 1 opt 2 opt 3 opt 4
let text = <...list.options>.find(o=> o.value=="2").text;console.log(text); Option A Option B Option C
Highly active question. Earn 10 reputation (not counting the association bonus) in order to lớn answer this question. The reputation requirement helps protect this question from spam and non-answer activity.
How vì chưng you remove all the options of a select box and then địa chỉ one option và select it with jQuery?
Site kiến thiết / biệu tượng công ty © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. Rev2022.4.21.42004