Multiple jquery selectors

It:

$('input.people').attr('checked', false).filter('[name=toddS]').attr('checked', true);

select the checkbox with the class of people and the name toddS, unchecking all other fields.

How to add another name to the filter? I tried several different combinations and nothing works. This is what I have:

$('input.people').attr('checked', false).filter('[name=toddS], [name=gwenD]').attr('checked', true);
+2
source share
2 answers

You can pass a function to .attr(), for example:

$('input.people').attr('checked', function() {
  return this.name == 'toddS' || this.name == 'gwenD';
});

If you need to add later, you can use something that works for more values, for example $.inArray(), for example:

$('input.people').attr('checked', function() {
  return $.inArray(this.name, ['toddS', 'gwenD']) != -1;
});
+1
source

You can use the function there to select the ones you want.

$('input.people')
   .removeAttr('checked')
   .filter(function() {
       return ($(this).attr('name') === 'toddS' || $(this).attr('name') === 'gwenD');
    })
    .attr('checked', 'checked');

removeAttr() false. , jQuery, false, HTML, , .

0

All Articles