jQuery Has Attribute [name]

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

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).

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("[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
CallWhat it does
[name]Learn the jQuery [attribute] selector: match elements that have a named attribute with any value, official div[id] demo, compare with [name=\

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Choose the attribute you care about (id, href, required, …).
Step 2Write $("[attr]") — or with a tag: $("div[id]").
Step 3Do not put a value inside the brackets unless you want exact equality.

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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: Attribute Equals Selector

Match attributes whose value equals a string exactly.

Attribute Equals Selector tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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