jQuery Has Attribute [name]

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is [name]?

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
UsePrefer
Attribute present$("[attr]")
Exact value$("[attr='value']")
Scoped tag$("a[href]") / $("input[required]")

How to Use [name]

Name the attribute, optionally add a tag, then query for presence only.

  1. Choose the attribute you care about (id, href, required, …).
  2. Write $("[attr]") — or with a tag: $("div[id]").
  3. Do not put a value inside the brackets unless you want exact equality.
  4. Prefer a tag prefix on large pages for clarity and a smaller match set.
PieceRequired?Role
attributeYesAttribute name that must be present
Tag nameOptionalLimits the search (e.g. div[id])
ValueNoOmitted 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)

Cheat Sheet

Keep these facts handy while checking attribute presence.

TopicDetail
Syntax$("[attr]")
MatchesAttribute present (any value)
Classic use$("div[id]")
Empty valueattr="" still matches
vs == requires an exact value
Common formsa[href], img[alt], input[required]
SincejQuery 1.0 (standard CSS)

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.

Result
$("div[id]")
matched: 3
labels: with id, empty id, has an id

/* presence — any id value */

jQuery Examples

Short, copy-ready snippets. Open a browser with jQuery loaded, or use Try It Yourself for a live editor.

Example 1 — Official Demo: List div[id]

Select every div that has an id, then list those ids (official idea).

jQuery
var ids = $("div[id]").map(function () {
  return this.id || "(empty)";
}).get().join(", ");

console.log(ids);
console.log("count:", $("div[id]").length);
Try It Yourself

How It Works

  1. Limit to divs. The div tag keeps the search focused.
  2. Require an id. [id] keeps only nodes that have the attribute.
  3. Map values. Reading this.id lists which ids were found.
  4. Official idea. Same presence check as the jQuery API div[id] demo.

Example 2 — [id] vs [id="…"]

Presence matches every id; equals matches only one exact id.

jQuery
console.log("[id]       :", $("div[id]").length);
console.log("[id=box-a] :", $("div[id='box-a']").length);

// presence is wider; equals is exact
Try It Yourself

How It Works

  1. Presence rule. [id] accepts any id value.
  2. Equals rule. [id='box-a'] needs that exact string.
  3. Compare counts. Lengths show how much wider presence is.
  4. Pick carefully. Any id → [id]. One known id → = or #id.

Example 3 — Select Links That Have href

Skip anchors used as buttons without a real URL.

jQuery
var hrefs = $("a[href]").map(function () {
  return $(this).attr("href");
}).get().join(", ");

console.log(hrefs);
console.log("count:", $("a[href]").length);
Try It Yourself

How It Works

  1. Target anchors. a[href] only considers links.
  2. Require href. Presence filters out placeholder anchors.
  3. Collect URLs. Mapping prints each matching href.
  4. Practical use. Count or style real navigation links only.

Example 4 — Find Required Fields with input[required]

Boolean attributes are a natural fit for presence matching.

jQuery
var names = $("input[required]").map(function () {
  return this.name || "(no name)";
}).get().join(", ");

console.log(names);
console.log("required count:", $("input[required]").length);
Try It Yourself

How It Works

  1. Boolean attribute. required in markup means the attribute is present.
  2. Select by presence. input[required] finds those fields.
  3. List names. Mapping helps you see which controls are required.
  4. Form habit. Useful for highlighting or validating required inputs.

Example 5 — Empty Attribute Values Still Match

If the attribute appears in HTML — even as attr="" — presence matches.

jQuery
console.log("div[id]:", $("div[id]").length);
console.log("empty id in set?", $("div[id='']").length);

// id="" has the attribute → matches [id]
Try It Yourself

How It Works

  1. Attribute exists. id="" is still an id attribute in the DOM.
  2. Presence matches. [id] includes that element.
  3. Equals empty. [id=''] can target only the empty ones.
  4. 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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All 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.

Performance Notes

ApproachCostBest for
$("div[id]")Native-friendly CSSScoped presence checks
$("a[href]")Native-friendly CSSReal links vs placeholder anchors
$("[id]")Wider scanAvoid 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.

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.

Next: [name="value"]

Match attributes whose value equals a string exactly.

= selector tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful