Cover Image for jQuery removeAttr() method
58 views

jQuery removeAttr() method

The jQuery removeAttr() method is used to remove one or more attributes from HTML elements. It allows you to remove specific attributes that have been previously set on the selected elements.

Here’s the basic syntax of the removeAttr() method:

$(selector).removeAttr(attributeName)

Parameters:

  • selector: A selector string or jQuery object representing the elements from which the attribute(s) will be removed.
  • attributeName: A string representing the name of the attribute to be removed.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>jQuery removeAttr() Method Example</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
  <button id="myButton" disabled>Click Me</button>

  <script>
    $(document).ready(function() {
      // Remove the "disabled" attribute from the button
      $("#myButton").removeAttr("disabled");
    });
  </script>
</body>
</html>

In this example, we have a button element with the ID “myButton” and the “disabled” attribute set. We use the removeAttr() method to remove the “disabled” attribute from the button, making it clickable again.

After executing the script, the “disabled” attribute is removed from the button, and the button becomes clickable.

You can use the removeAttr() method with any HTML attribute that you want to remove from the selected elements. It’s commonly used when you want to reset or modify the attributes of elements dynamically in response to user actions or other events.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS