Categories

.removeAttr()

Categories: General Attributes | Attributes

.removeAttr( attributeName )Returns: jQuery

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

  • .removeAttr( attributeName )

    version added: 1.0

    attributeName   An 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);
  • Clicking the button enables the input next to it.

    HTML:
    <button>Enable</button>
    <input type="text" title="hello there" />
    <div id="log"></div>
    
    Code:
    
    (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") );
      });
    })();
    

.removeAttr( attributeName )

Plugin: ep.modify

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

  • .removeAttr( attributeName )

    version added: 6.11.0

    attributeName   The name of the attribute to set.

The ep.modify plugin overwrites the .removeAttr() method, is works like the original method but fire an event named changeAttr.

  • Bind changeAttr event and remove an attribute.

    HTML:
    <div>
      <input type="checkbox" name="foo" selected="true"/>
      <span style="display:none;">This field is required.</span>
    </span>
    
    Code:
    $(':checkbox').on( 'changeAttr', function( event ){
        var elem = $(this);
        if( elem.is(':selected') ){
            elem.next().hide();
        }
        else{
            elem.next().show();
        }
    });
    
    $(':checkbox').removeAttr( 'selected' );
    
    Results:
    <div>
      <input type="checkbox" name="foo"/>
      <span>This field is required.</span>
    </span>
    
    
  • changeAttr

    version added: 6.11.0

    Gets fired if an attribute(s) of an element changed.