I've seen on StackOverflow and googling around that the most used way to get the selected text from a <SELECT>
element with jquery is like this
$("#cboId :selected").text()
what I have is not the id, but an object. I have an object, say
var myCombo= $("#cboId");
coming from an earlier piece of code.
I've done like this to get the selected text, but is ugly:
$("#" + myCombo.attr('id') + " :selected").text() ;
is there any cleaner way to do it?
thanks.
You can select on any attribute and its value by using the attribute selector [attributename=optionalvalue] , so in your case you can select the option and set the selected attribute. $("div. id_100 > select > option[value=" + value + "]"). prop("selected",true);
Select the <select> element using JQuery selector. This selector is more specific and selecting the first element using option:nth-child(1). This will get access to the first element (Index starts with 1).
Method 1: Append the option tag to the select box The option to be added is created like a normal HTML string. The select box is selected with the jQuery selector and this option is added with the append() method.
var selectedText = $(":selected",myCombo).text();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With