The has-attribute selector — written [name] — matches elements that have the named attribute, regardless of its value. It is the simplest attribute filter and the foundation before [name="value"] exact matching. Standard CSS since jQuery 1.0.
01
Syntax
[attr]
02
Presence
Any value
03
Official
div[id]
04
vs =
Not exact
05
Combine
input[required]
06
Since 1.0
Standard CSS
Fundamentals
Introduction
Sometimes you care whether an attribute exists at all — not what it equals. Links with an href, images with alt, inputs marked required, or any element with an id. The CSS presence selector [attribute] answers that question in one concise expression.
jQuery has supported [attribute] since version 1.0. Official documentation describes it as selecting elements that have the specified attribute with any value. It works in stylesheets and in jQuery selector strings — a natural first step before learning equals, prefix, and substring operators.
Concept
Understanding the Has-Attribute Selector
Think of [attribute] as “does this element carry this attribute name?”:
attribute — the HTML attribute name to test for presence (id, href, required, etc.).
Return value
A jQuery object containing every element that has the named attribute.
Empty collection when no elements in scope carry that attribute.
Official jQuery API example
jQuery
$( "div[id]" ).one( "click", function() {
var idString = $( this ).text() + " = " + $( this ).attr( "id" );
$( this ).text( idString );
});
Cheat Sheet
⚡ Quick Reference
Markup
[id]
[id="foo"]
id="foo"
Matches
Matches
id="bar"
Matches
No match
No id attribute
No match
No match
required (no value)
input[required] matches
N/A
Native CSS
Supported
Supported
Compare
📋 [name] vs [name="value"] and other operators
Attribute presence vs exact, substring, and prefix matching.
[attr]
div[id]
Has attribute
[attr="x"]
[id="foo"]
Exact value
[attr*="x"]
[href*="pdf"]
Contains substring
[attr^="x"]
[href^="https"]
Starts with
Hands-On
Examples Gallery
Example 1 follows the official jQuery div[id] one-click demo. Examples 2–5 target required inputs, links with href, contrast with exact match, and highlight images with alt.
📚 Official jQuery Demo
One click on divs with an id appends the id value to the text.
Example 1 — Official Demo: div[id]
Official jQuery demo — click divs that have an id; text becomes “label = idValue”. Divs without id are ignored.
jQuery
$( "div[id]" ).one( "click", function() {
var idString = $( this ).text() + " = " + $( this ).attr( "id" );
$( this ).text( idString );
});
Photo with alt → green outline
Decorative image without alt → red outline
How It Works
img[alt] matches when the alt attribute exists. Pair with :not([alt]) to find images missing descriptions.
Applications
🚀 Common Use Cases
IDs present — bind handlers to div[id] as in the official demo.
Form validation — highlight input[required] fields.
Real links — style a[href] separately from placeholder anchors.
Accessibility — audit img[alt] and input[aria-label].
Data attributes — find [data-id] before reading values with .data().
CSS parity — same selector in stylesheets and jQuery.
🧠 How jQuery Evaluates [attribute]
1
Parse selector
Combine optional type selector with [attribute] — e.g. div[id].
Query
2
Check presence
For each candidate, verify the named attribute exists in the DOM — any value counts.
CSS
3
Collect matches
All elements with that attribute name in scope are returned.
Set
4
✓
Return collection
Chain .css(), .one(), or .attr() on matched nodes.
Important
📝 Notes
Available since jQuery 1.0 — standard CSS attribute presence selector.
Matches any value — including empty string if the attribute is present in markup.
Foundation before [attr="value"], [attr*="value"], and other operators.
Combine with element type — input[required] not bare [required] on large pages.
Works in native querySelectorAll("a[href]") in modern browsers.
Does not match attributes added only via JavaScript until reflected in the DOM attribute map jQuery queries.
Compatibility
Browser Support
The [attribute] has-attribute selector is standard CSS and works in jQuery 1.0+. All modern browsers support document.querySelectorAll("input[required]"). Same syntax in stylesheets and jQuery selectors.
Bottom line: Use div[id] or input[required] — presence before exact [name="value"] matching.
Wrap Up
Conclusion
The [attribute] selector matches every element that has the named attribute — the official div[id] demo reveals each id on click, ignoring divs without an id.
Use it for presence checks, then graduate to [name="value"] and other operators when you need to match specific attribute values.
It selects elements that have the specified attribute present in the markup — with any value, including an empty string. $("div[id]") matches every div that has an id attribute, regardless of what the id value is. Available since jQuery 1.0.
[attribute] checks only for presence of the attribute. [attribute="value"] requires the attribute value to equal a specific string exactly. A div with id="foo" matches both div[id] and div[id="foo"], but a div without an id matches neither.
Yes. If the attribute appears in the HTML — even as attr="" — the element has that attribute and matches [attribute]. The selector does not inspect whether the value is useful; it only checks existence.
Yes. The has-attribute selector is standard CSS and works in native querySelectorAll — e.g. document.querySelectorAll("a[href]"). jQuery has supported it since 1.0.
Both can work for boolean attributes. [required] is useful in selector strings and CSS stylesheets — e.g. form input[required] { border-color: red; }. Use .prop() when you need live property state that may differ from the initial markup.
Yes — and you should for clarity. input[name], a[href], and img[alt] are common patterns. They are faster and clearer than bare [name] alone on large pages.
Did you know?
The has-attribute selector is the base of the attribute selector family. CSS adds operators after the attribute name — =, *=, ^=, $=, |=, and ~= — each narrowing matches by value. Master [name] first, then learn the operators in the other tutorials.