JQuery parameter set to switch using id and class switches

Is it possible to set a switch for checking with jquery - class and id?

For example:

$('input:radio[class=test1 id=test2]).attr('checked', true); 

I can only install it by id OR, but not both.

+54
jquery checked button
Apr 11 '13 at
source share
1 answer

"... by class and div."

I assume that when you say "div", you mean "id"? Try the following:

 $('#test2.test1').prop('checked', true); 

No need to guess with the [attributename=value] style selectors, because id has its own format , like class , and they are easy to combine, although, given that the identifier must be unique, it should be sufficient for yourself, if only your the meaning is not "select this element only if it currently has the specified class."

Or, as a rule, to select the input where you want to specify several attribute selectors :

 $('input:radio[class=test1][id=test2]').prop('checked', true); 

That is, specify each attribute with its square brackets.

Note that if you do not have a fairly old version of jQuery, you should use .prop() rather than .attr() for this purpose.

+125
Apr 11 '13 at
source share



All Articles