jQuery Has Attribute [name] Selector

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Presence · CSS · jQuery 1.0+

What You’ll Learn

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

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.

Understanding the Has-Attribute Selector

Think of [attribute] as “does this element carry this attribute name?”:

  • <div id="foo"> + div[id] → matches.
  • <div> (no id) + div[id] → no match.
  • <input required> + input[required] → matches (boolean attribute).
  • id="foo" matches [id] and [id="foo"] — presence vs exact value.
💡
Beginner Tip

When you need a specific value, step up to [attr="value"]. When you only need to know the attribute is there — use [attr].

📝 Syntax

Official jQuery API form (since 1.0):

jQuery
jQuery( "[attribute]" )

// typical usage — combine with tag name:
$( "div[id]" )
$( "input[required]" )
$( "a[href]" )
$( "img[alt]" )

Parameters

  • 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 );
});

⚡ Quick Reference

Markup[id][id="foo"]
id="foo"MatchesMatches
id="bar"MatchesNo match
No id attributeNo matchNo match
required (no value)input[required] matchesN/A
Native CSSSupportedSupported

📋 [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

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 );
});
Try It Yourself

How It Works

.one() fires at most once per element. Only divs with an id attribute match div[id] — the value can be anything.

Example 2 — Required Fields: input[required]

Style every input that has the boolean required attribute.

jQuery
$( "input[required]" ).css({
  borderLeft: "4px solid #dc2626"
});
Try It Yourself

How It Works

Boolean attributes like required match [required] when present in HTML — no = value needed.

📈 Practical Patterns

Links, exact comparison, and accessibility checks.

Example 3 — Links With href: a[href]

Count and underline anchors that have an href attribute — real navigation links.

jQuery
$( "a[href]" ).css( "textDecoration", "underline" );
console.log( "Links with href:", $( "a[href]" ).length );
Try It Yourself

How It Works

a[href] matches any anchor with an href — #, relative paths, or full URLs. Placeholder <a> tags without href are excluded.

Example 4 — Presence vs Exact: [id] and [id="foo"]

Show how presence matches many ids while equals matches one value.

jQuery
console.log( "Has id →", $( "div[id]" ).length );
console.log( 'id="foo" →', $( 'div[id="foo"]' ).length );
Try It Yourself

How It Works

Presence is broader — any id value matches [id]. Exact match narrows to one specific token.

Example 5 — Accessibility: img[alt]

Highlight images that include alt text — a quick audit for missing descriptions.

jQuery
$( "img[alt]" ).css( "outline", "3px solid #16a34a" );
$( "img:not([alt])" ).css( "outline", "3px solid #dc2626" );
Try It Yourself

How It Works

img[alt] matches when the alt attribute exists. Pair with :not([alt]) to find images missing descriptions.

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

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

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.

CSS · jQuery 1.0+

jQuery Has Attribute [name] Selector

Universal browser support. Native: querySelectorAll("div[id]").

100% Standard CSS
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
[name] Universal

Bottom line: Use div[id] or input[required] — presence before exact [name="value"] matching.

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.

💡 Best Practices

✅ Do

  • Prefix with element type — a[href], img[alt]
  • Use [attr] when any value is acceptable
  • Step up to [attr="value"] for exact tokens
  • Reuse the same selector in CSS and jQuery
  • Pair with :not([attr]) to find missing attributes

❌ Don’t

  • Use [attr="x"] when [attr] is enough — too strict
  • Scan with bare [attribute] on huge documents
  • Assume presence implies a non-empty value
  • Confuse HTML attributes with jQuery .data() keys
  • Forget boolean attributes match with no = value

Key Takeaways

Knowledge Unlocked

Five things to remember about [name]

Attribute presence, any value.

5
Core concepts
any 02

Any value

Not exact

Rule
demo 03

Official

div[id]

Demo
= 04

Next step

[attr="x"]

Path
CSS 05

Standard

Native CSS

Tip

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

Continue to [name="value"] Selector

After presence matching, learn exact attribute equality with the equals operator.

Attribute equals 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.

6 people found this page helpful