JQuery matches multiple attributes

I have the following markup and I want to set the All switch.

 <ul> <li><input type="radio" value="All" name="Foo"/>All</li> <li><input type="radio" value="New" name="Foo"/>New</li> <li><input type="radio" value="Removed" name="Foo"/>Removed</li> <li><input type="radio" value="Updated" name="Foo"/>Updated</li> </ul> 

I would like to map an attribute through an attribute, but I need to combine 2 attributes, @name='Foo' and @value='All' .

Something like that:

 $("input[@name='Foo' @value='all']").attr('checked','checked'); 

Can anyone show how this can be done?

+45
jquery html jquery-selectors
01 Oct '08 at 2:21
source share
2 answers

The following HTML file shows how you can do this:

 <html> <head> <script type="text/javascript" src="jquery-1.2.6.pack.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("a").click(function(event){ $("input[name='Foo'][value='All']").attr('checked','checked'); event.preventDefault(); }); }); </script> </head> <body> <ul> <li><input type="radio" value="All" name="Foo" />All</li> <li><input type="radio" value="New" name="Foo" />New</li> <li><input type="radio" value="Removed" name="Foo" />Removed</li> <li><input type="radio" value="Updated" name="Foo" />Updated</li> </ul> <a href="" >Click here</a> </body> </html> 

When you click on the link, the desired radio button is selected. An important line is setting the checked attribute.

+67
Oct 01 '08 at 2:43
source share

I hit my head against a wall like this, and just want to point out that in jQuery 1.3 the syntax used in the accepted answer is the only syntax that will work. The questionnaire uses @ syntax for an expression that does not work at all in jQuery. Hope this helps the next guy find this question via google = p

To be clear you should use

 jQuery('input[name=field1][val=checked]') 

but not

 jQuery('input[@name=field1][@val=checked]') 
+13
Apr 21 '09 at 8:31
source share



All Articles