Has Attribute [attr]
JQ 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]
Examples
Selectors << Top
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 apart from those in tables).
$(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');
});
});
Related Tutorials
jQuery Basic Tutorials - Lesson 3 - CSS Selectors