tags:

views:

83

answers:

4

Hi there,

I need to check if a select has an option whose TEXT is a specific value.

Eg <option value="123">abc</option> i would be looking for abc.

Is there a selector to do this?

Im looking for something similar to $('#select option[value="123"]'); but for text

A: 

you can use

$('MY SELECT option:selected').text();

here is a working example http://jsfiddle.net/nmvf6/

From.ME.to.YOU
I don't think that the OP asks for this.
dekomote
You're misinterpreting his question... read it again, slowly this time.
Yi Jiang
Don't think you fully understood the OPs question.
mkoistinen
A: 

Either you iterate through the options, or put the same text inside another attribute of the option and select with that.

dekomote
+4  A: 

You can use the :contains() selector to select elements that contain specific text.
For example:

$('#mySelect option:contains(abc)')

To check whether a given <select> element has such an option, use the .has() method:

if (mySelect.has('option:contains(abc)').length)

To find all <select>s that contain such an option, use the :has() selector:

$('select:has(option:contains(abc))')
SLaks
`:contains` is not definitive now, is it?
Floyd Pink
@Floyd: ​​What?
SLaks
check my answer below
Floyd Pink
+2  A: 

This could help:

$('#test').find('option[text="B"]').val();

Demo - http://jsfiddle.net/nmvf6/1/

This would give you the option with text B and not the ones which has text that contains B. Hope this helps

Floyd Pink