Has Attribute [attr]
S2C Home « Selectors « Has Attribute [attr]
Has attribute selector.
Shorthand version $('[attr]')
Description
The [attr]
selector, selects all elements that have the specified attribute name.
Syntax
Signature | Description |
---|---|
jQuery('[attr]') | Attribute match |
Parameters
Parameter | Description |
---|---|
attr | The attribute name which can be either an unquoted single word or a quoted string. |
Return
N/A.
Has Attribute [attr]
ExamplesTop
Selects all elements that have the specified attribute name.
The following example will select all 'href' attributes on the page and change their color to purple (all links on the page not marked with !important, the links at top and bottom of page).
$(function(){
$('#btn1').on('click', function() {
$('[href]').css('color', 'purple');
});
});
The following example will select all 'type' attributes on the page and change their background color to orange (all button elements).
$(function(){
$('#btn2').on('click', function() {
$('[form]').css('backgroundColor', 'orange');
});
});
The following example will select all 'action' attributes on the page and change their background color to orange (all form elements and search box).
$(function(){
$('#btn3').on('click', function() {
$('[action]').css('backgroundColor', 'yellow');
});
});