The has-attribute selector matches elements that have a named attribute in the markup, regardless of its value. Available since jQuery 1.0 (standard CSS).
Remember
$("[attribute]")
$("div[id]")
✓ <div id="a">…</div>
✓ <div id="">…</div> (empty still has the attr)
✗ <div>…</div> (no id attribute)
[attr] ≠ [attr='value'] // presence vs exact value
Name the attribute, optionally add a tag, then query for presence only.
Choose the attribute you care about (id, href, required, …).
Write $("[attr]") — or with a tag: $("div[id]").
Do not put a value inside the brackets unless you want exact equality.
Prefer a tag prefix on large pages for clarity and a smaller match set.
Piece
Required?
Role
attribute
Yes
Attribute name that must be present
Tag name
Optional
Limits the search (e.g. div[id])
Value
No
Omitted on purpose — any value matches
Pseudocode
Pseudocode
attrName = given attribute
matches = empty list
for each element:
if element.hasAttribute(attrName): // any value, including ""
add element
return jQuery(matches)
Reference
Cheat Sheet
Keep these facts handy while checking attribute presence.
Topic
Detail
Syntax
$("[attr]")
Matches
Attribute present (any value)
Classic use
$("div[id]")
Empty value
attr="" still matches
vs =
= requires an exact value
Common forms
a[href], img[alt], input[required]
Since
jQuery 1.0 (standard CSS)
Try it
Live Preview
Run presence / equals selectors against sample divs and see which elements match.
Sample markup mirrors the official jQuery div[id] demo, plus href/required examples.
div[id]: 2
empty id in set? 1
Empty string still counts as having the attribute
How It Works
Attribute exists.id="" is still an id attribute in the DOM.
Presence matches.[id] includes that element.
Equals empty.[id=''] can target only the empty ones.
Takeaway. Presence ≠ “has a useful value.”
Edge Cases & Pitfalls
Check these before relying on [attribute] in real projects.
empty
Empty values still match
attr="" means the attribute exists. If you need a non-empty value, combine with a value selector or filter in code.
vs =
Presence ≠ exact value
[id] is wider than [id="box-a"]. Use equals (or #id) when you know the value.
scope
Bare [attr] can be broad
Prefer div[id], a[href], or a container context on large pages.
prop
Markup vs live property
For boolean attributes toggled later, .prop("required") may reflect runtime state better than the initial [required] selector alone.
Compatibility
Browser Support
The has-attribute selector [attr] is standard CSS and has worked in jQuery since 1.0. Modern browsers implement it in native querySelectorAll.
✓ CSS · jQuery 1.0+
jQuery [name]
Supported in all modern browsers. Native: document.querySelectorAll("div[id]"). Matches attribute presence with any value.
100%Standard CSS selector
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
[attr]Universal
Bottom line: Safe everywhere jQuery runs. Use for presence checks; switch to [attr="value"] when the exact value matters.
Analysis
Performance Notes
Approach
Cost
Best for
$("div[id]")
Native-friendly CSS
Scoped presence checks
$("a[href]")
Native-friendly CSS
Real links vs placeholder anchors
$("[id]")
Wider scan
Avoid on large pages without a tag/context
Attribute presence selectors are standard CSS, so jQuery can often use the browser engine. Prefer a tag prefix or a container on large documents.
Remember
Key Takeaways
Role: match elements that have a named attribute with any value.
Classic use:$("div[id]") from the official demo.
Empty counts:attr="" still has the attribute.
Not equals: for a known value, use [attr="value"].
One line: use [attr] to select by attribute presence — any value, including empty.
Frequently Asked Questions
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. [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 only checks existence, not whether the value is useful.
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.
[required] is useful in selector strings and stylesheets. 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 clearer than bare [name] alone on large pages.