admin管理员组

文章数量:1394759

I have multiple removeAttr() is there a way I can bine them?

What I have: (this works)

checkboxes.removeAttr("checked",'checked');
checkboxes.removeAttr("indeterminate", false);
checkboxes.removeAttr("role", 'checkbox');
checkboxes.removeAttr("aria-checked", false);

What I have tried: (this doesn't work)

checkboxes.removeAttr({
    checked: 'checked',
    indeterminate: false,
    role: 'checkbox',
    'aria-checked': false
});

When I try this I get the following console error. So either my syntax is wrong or this just doesn't work.

Uncaught TypeError: Object #<Object> has no method 'toLowerCase'  jquery.min.js:2

Any help is appreciated. Thanks.

I have multiple removeAttr() is there a way I can bine them?

What I have: (this works)

checkboxes.removeAttr("checked",'checked');
checkboxes.removeAttr("indeterminate", false);
checkboxes.removeAttr("role", 'checkbox');
checkboxes.removeAttr("aria-checked", false);

What I have tried: (this doesn't work)

checkboxes.removeAttr({
    checked: 'checked',
    indeterminate: false,
    role: 'checkbox',
    'aria-checked': false
});

When I try this I get the following console error. So either my syntax is wrong or this just doesn't work.

Uncaught TypeError: Object #<Object> has no method 'toLowerCase'  jquery.min.js:2

Any help is appreciated. Thanks.

Share Improve this question edited Feb 9, 2016 at 16:51 khollenbeck asked Feb 28, 2013 at 16:46 khollenbeckkhollenbeck 16.2k18 gold badges68 silver badges102 bronze badges 2
  • 2 .removeAttr() only takes one parameter. An attribute to remove; as of version 1.7, it can be a space-separated list of attributes. – gen_Eric Commented Feb 28, 2013 at 16:49
  • Aren't you really looking for .prop()? – j08691 Commented Feb 28, 2013 at 16:49
Add a ment  | 

4 Answers 4

Reset to default 5

Just use $.each with an array, quick, short, and expandable.

$.each(["checked","indeterminate"],function(i,attrName){
    checkbox.removeAttr(attrName);
});

According to removeAttr() doc it does not accept an object. so

checkboxes.removeAttr({}); will not work. and it also accept one paremeter [thx @RocketHazmat] for correction to which you want to remove.

You can do a chaining like:

checkboxes
   .removeAttr("checked")
   .removeAttr("indeterminate")
   .removeAttr("role")
   .removeAttr("aria-checked");

But if you want to set attribute then you've to use attr() method like:

checkboxes
  .attr("checked", 'checked')
  .attr("indeterminate",false )
  .attr("role",'checkbox')
  .attr("aria-checked", false);

I'm not sure why you're passing both keys and values to removeAttr, but if you just want to remove those 4 attributes from an element, pass a space-separated list of the keys, like so:

checkboxes.removeAttr("checked indeterminate role aria-checked");

@RocketHazmat is right, so it would be like this:

checkboxes.removeAttr("checked indeterminate role aria-checked");

But you need 1.7 or later from jQuery.

本文标签: javascriptCombing multiple removeAttr() statementsStack Overflow