views:

497

answers:

2

I'm trying to select elements with multiple conditions, for example I'm doing the following at the moment:

$('#myspan').find('input:visible').each(myfunc);

Although I know you can do things like $('#myspan input:visible') but it didn't work for me. I need to check for inputs within the span #myspan which are visible and are checked. Any ideas?

+2  A: 
$('input:visible', '#myspan').find(':checked').each(function() {
    alert(this.id);
});

Should do the trick. I like seperating things because I like to think it helps jQuery parse better.

Paolo Bergantino
Dang you're fast. Thanks!
Kezzer
It *is* faster to specify a context. Don't know why, but it seems to be so.
Seb
@Seb, the why is quite simple: without a context, the search starts from the root of the document ;)
Julian Aubourg
+3  A: 

Try:

$("#myspan :checked:visible").each(function() {
    // do stuff
});
cletus
Gah, if only I could have multiple correct answers ;) This one's good too. How come you ommitted the input though?
Kezzer
You can put it in but it's implied by :checked (since only checkboxes can be checked and only inputs can be checkboxes). Up to you thought. I prefer terser expressions but putting in input:checked:visible is fine.
cletus