dev1234
dev1234

Reputation: 5716

How to get HTML select tag elements, option text not value

I know how to get the value of a selected option but i am wondering how to get its text.

this is how i access the option using the value ?

$('#dropdownId').children('option[value="'+5691+'"]').hide();

something like below,

$('#dropdownId').children('option[text="'+'some text'+'"]').hide();

Upvotes: 0

Views: 1161

Answers (5)

Furquan Khan
Furquan Khan

Reputation: 1594

I guess you are trying to achieve some thing like this

if($('#dropdownId').children().text()=="some text")
   $('#dropdownId').children().hide();

Thanks.

Upvotes: 0

Nono
Nono

Reputation: 7302

Try this:

HTML:

<select id="foo">
    <option value="1">foo</option>
    <option value="2">bar</option>
</select>

Java Script:

$(function () {
    $("#foo").change(function(){
        alert($("#foo option:selected").text());
    });    
});

Upvotes: 0

vusan
vusan

Reputation: 5331

try:

$('#dropdownId').children('option[value="'+5691+'"]').text()

Upvotes: 2

Pankucins
Pankucins

Reputation: 1720

Use .text().

Here's a fiddle - http://jsfiddle.net/friiks/XXsFy/

Upvotes: 1

YD1m
YD1m

Reputation: 5895

Use this:

.children("option:contains('"+text+"')")

Upvotes: 1

Related Questions