views:

61

answers:

2

Annoyingly enough, setting the cssClass property of an ASP.NET checkbox control renders HTML like so instead of just adding the class attribute to the input tag:

<span class="myclass">
    <input id="chkBox1" type="checkbox" name="chkBox1" />
 </span>

So here I am trying to write a Jquery filter that finds all of the checked checkboxes that are nested inside a span tag with the class "myclass" specified.

This is what I have so far, but it doesn't seem to be working.

$(".myclass input[type='checkbox']:checked")

Anyone see what I am doing incorrectly?

I'd also accept a solution that tells me how to make the checkbox control just put the dang class attribute on the input control instead of wrapping a span around it.

+1  A: 

I actually don't think you need the input type because only a checkbox can be true for :checked so you could write $(".myclass").find("input:checked") and it should probably be fine

jarrett
Radio-button can also be checked.
DreamSonic
Thanks for clarifying.
jarrett
An equally good answer, but I accepted the other one because the selector style works better in my particular situation than the .find syntax. Definitely still worth the upvote (+1).
JohnFx
+2  A: 

This should work:

$("span.myclass :checkbox")

Do you also need them checked? Then like so:

$("span.myclass :checkbox:checked")
DreamSonic
That did the trick. Thanks!Just so I understand, though. I thought that adding a space in a selector basically equated to an OR for the attributes on either side of the space. Am I mistaken?
JohnFx
Space means nesting. See docs: http://docs.jquery.com/Selectors/descendant#ancestordescendant
DreamSonic