views:

55

answers:

1

I want to get all inputs that are checked and not hidden

This doesn't work:

.formyform input:checked:not(:hidden)

Is there a simple way to do it?

I couldn't find any example on the jQuery site

+2  A: 

What you have should work, though you can simplify it with :visible:

.formyform input:checked:visible

You can test it out here.

Nick Craver
Does `input` serve a purpose? A little shorter is `$(".formyform :checked:visible")` - (note the space before the first colon).
Peter Ajtai
@Peter - It makes the selector much faster by performing a `.getElementsByTagName()` under neath, rather than checking just attribute and dimension selectors on all children :)
Nick Craver
Thanks for the explanation Nick. I wonder how big of a difference it makes in practice, since I assume the dimensions aren't looked at unless the element is found to be checked, and `getElementsByTagName()` still has to look through all the elements once.
Peter Ajtai
@Peter - Native code vs a loop in JavaScript is a huge difference, you're talking about JavaScript getting *all* elements underneath and checking the `.checked` attribute, whereas `.getElementsByTagName()` is a native code call returning only the `<input>` elements to loop though. The bigger the DOM the greater the difference of course, don't trust me though, test the difference yourself: http://jsperf.com/element-selector-test
Nick Craver
Wow. I didn't realize how much faster `.getElementsByTagName()` made that. I'd give you another +1 for that link if I could. I didn't know about that site. Thanks!
Peter Ajtai
@Peter - Welcome :) I thought more knew about it...if performance questions come up I'll try and use it more to get the word out, it is indeed an excellent tool.
Nick Craver