![Logo](/_astro/logo.DNC1PCTe_Z1abT04.webp)
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');
Gets or sets properties on an element.
Component Properties - There are special helpers
setting
andsettings
to set component properties for web components.
$('selector').prop(propertyName)
$('selector').prop(propertyName, value)$('selector').prop({property1: value1, property2: value2})
Name | Type | Description |
---|---|---|
propertyName | string | The name of the property to set |
value | any | The value to set for the property |
Query object (for chaining).
// Get a property valueconst isChecked = $('input[type="checkbox"]').prop('checked');console.log(isChecked);
// Set a single property$('input[type="checkbox"]').prop('checked', true);
// Set multiple properties$('select').prop({ disabled: false, selectedIndex: 0});