views:

88

answers:

4
+3  Q: 

jQuery Selectors

I am trying to get an array of checkboxes that ARE checked AND have a certain attribute (tag) that is NOT blank.

(I know that the attribute called tag is not HTML compliant but it does work across the major browsers and makes life alot simpler!)

e.g.

<input type="checkbox" name="First" tag="[email protected]" checked="checked">
<input type="checkbox" name="Second" tag="" checked="checked">

In the above example, only the first one should be in the array The code I have started with is:

$('input:checkbox:checked')

But that obviously doesnt ensure that the tag attribute is not blank

UPDATE
Sorry, something I didnt think of before - I also need to get a list of the checkboxes whose tag is blank so the user can be warned. The following dont work:

$('input:checkbox:checked[title=""]')
$('input:checkbox:checked:not([title])')

Thanks for all the responses, much appreciated!

UPDATE 2

The above code (using title to store the additional data) doesnt work purely it seems because of the title atribute! When I changed it to tag it worked perfectly.

+3  A: 

attributes use the [tag="[email protected]"] syntax in JQuery; try appending that as in:

$('input:checkbox[tag="[email protected]"]:checked')

The other option is you may want to check out the data method in JQuery, which can append data to an HTML element, instead of using the tag attribute: http://docs.jquery.com/Data

Brian
+2  A: 
$("input:checkbox[tag!='']:checked")
elektronikLexikon
+3  A: 

HTML compliant

HTML:

<input type='checkbox' alt='bleh' checked='checked' id='1' />
<input type='checkbox' alt='' checked='checked' id='2' />

And Selector:

$('input:checkbox:checked[alt!=""]')

http://jsfiddle.net/LVqLw/

Robert
+2  A: 

You can simply use [tag] to test if it is blank or if the tag exists at all.

Example: http://jsfiddle.net/xCFfa/

$(":checkbox:checked[tag]");
patrick dw