0

I have a set of Yes/No dropdowns and I'd like to select all select elements with a value of Yes.

<select name="foo1">
  <option value="yes">Yes</option>
  <option value="no">No</option>
</select>
<select name="foo2">
  <option value="yes">Yes</option>
  <option value="no">No</option>
</select>
<select name="foo3">
  <option value="yes">Yes</option>
  <option value="no">No</option>
</select>

I'd like a JQuery selector that returns a subset of select nodes which are currently in a state of Yes. I realize I can do a simple call to filter(), but I would much rather do it all in a selector.

$('#myform select').filter(function(k,v){return v.value == 'yes';})

Thanks in advance.

1
  • Why? I would use filter method, faster and promising. Commented Feb 22, 2013 at 21:04

4 Answers 4

2
$('#myform select option[value="yes"]:selected').parent()

See demo

Sign up to request clarification or add additional context in comments.

1 Comment

Man, I could have sworn I tried this. I tried so many things, some completely off the wall. Who knows what I was doing in the end. Thx.
1

If you want it to be efficient, you should stick to filter, but you can create a custom filter that you can use directly in your selector:

$.expr[':'].value = $.expr.createPseudo(function (value) {
    return function (el) {
        return el.value == value;
    };
});

Use it as follows:

$('select:value(yes)');

Here's the fiddle: http://jsfiddle.net/Eb3hp/

1 Comment

This looks really awesome. I'm thinking overkill for my purpose, but I'll +1 for coolness value.
1

it would be

$('#form select:has(option[value=yes]:selected)')

It will get all select that has option:selected with a value=yes

FIDDLE

Comments

0
$('#myform select[value=yes]');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.