
ctrl+k
Enter a search term above to see results...
Enter a search term above to see results...
Attribute methods in Query allow you to get, set, and remove attributes on DOM elements.
Gets or sets attributes on elements.
$('selector').attr(attributeName)
$('selector').attr(attributeName, value)$('selector').attr({attribute1: value1, attribute2: value2})
Name | Type | Description |
---|---|---|
attributeName | string | The name of the attribute to get |
Query object (for chaining).
// Get an attribute valueconst href = $('a').attr('href');console.log(href);
// Set a single attribute$('img').attr('alt', 'Profile picture');
// Set multiple attributes$('input').attr({ 'type': 'text', 'placeholder': 'Enter your name'});
Removes an attribute from each element in the set of matched elements.
Boolean Attributes This is helpful for boolean attributes where setting a value like
checked="false"
will actually result in checked being set.
$('selector').removeAttr(attributeName)
Name | Type | Description |
---|---|---|
attributeName | string | The name of the attribute to remove |
Query object (for chaining).
// Remove the 'disabled' attribute from all buttons$('button').removeAttr('disabled');