jQuery API

.removeAttr()

.removeAttr( attributeName ) Returns: jQuery

Description: Remove an attribute from each element in the set of matched elements.

  • version added: 1.0.removeAttr( attributeName )

    attributeNameAn attribute to remove; as of version 1.7, it can be a space-separated list of attributes.

The .removeAttr() method uses the JavaScript removeAttribute() function, but it has the advantage of being able to be called directly on a jQuery object and it accounts for different attribute naming across browsers.

Note: Removing an inline onclick event handler using .removeAttr() doesn't achieve the desired effect in Internet Explorer 6, 7, or 8. To avoid potential problems, use .prop() instead:

$element.prop("onclick", null);
console.log("onclick property: ", $element[0].onclick);

Example:

Clicking the button enables the input next to it.

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <button>Enable</button>
<input type="text" title="hello there" />
<div id="log"></div>

<script>
(function() {
  var inputTitle = $("input").attr("title");
  $("button").click(function () {
    var input = $(this).next();

    if ( input.attr("title") == inputTitle ) {
      input.removeAttr("title")
    } else {
      input.attr("title", inputTitle);
    }

    $("#log").html( "input title is now " + input.attr("title") );
  });
})();
</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .removeAttr() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .removeAttr()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • Noorsulayman

    This does not work under IE7.
    e.g. $('#parent_div div').removeAttr('class'); In all other browsers, this removes the class=”something” string from the <div id=”parent_div”><div class=”something”></div></div> string.

    It has been stated over at http://gimp4you.eu.org/sandbox/js/test/removeAt… that you can remove the attribute like so: $('#parent_div div').attr('class', ''); This is a poor work around I think….

    • http://sergiobobillier.blogspot.com Sergio Bobillier

      That is because to remove a class you don't use the removeAttr method but the removeClass method. If you remove all the classes the item is left with no style.

  • profeval

    I’d like to know more about removing selected=”selected” from a specific option in a dropdown, since all attempts I’ve made have failed. I actually want the DOM to be updated to reflect the unselected item as well.

    • Littlewhore

      try to add the “selected” attribute on an other option…
      ex : $(‘#1 :first-child’).attr(‘selected’, true);

  • Persevere4triumph

    Interesting. The example shows that when .removeAttr('disabled') is run, it completely removes not just the attribute value, but the attribute as well.
    I need to remove an attribute of 'readonly' (not just the value), because it seems that readonly=” ” is the same as readonly=”readonly”…?

    • losewin

      look profeval's question and Littlewhore's answer
      .attr('selected', true)

      you should try
      .attr(“readOnly”,false)

  • Clarence L.

    I had an issue where IE8 won't allow me to removeAttr('colspan') from a td, that had a colspan previously set to 2. Using attr('colspan', 1) worked though.

  • Clarence L.

    I had an issue where IE8 won't allow me to removeAttr('colspan') from a td, that had a colspan previously set to 2. Using attr('colspan', 1) worked though.