Advanced Attribute Selectors
Explore complex attribute selectors to target elements based on their attributes' presence, value, or partial matches, enhancing your selection precision.
Advanced Attribute Selectors is a free jQuery Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the jQuery Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Hello, Attribute Selectors!
Welcome to a powerful way to select elements in jQuery! We'll dive into attribute selectors, which let you pick elements based on their HTML attributes.
Think of attributes like extra data attached to an HTML tag, such as id, class, href, data-*, and more.
Selecting by Attribute Presence
The simplest attribute selector finds elements that have a specific attribute, regardless of its value. It's written as [attributeName].
This is super useful when you just need to know if an attribute exists on an element. For example, $('[title]') will find all elements with a title attribute.
Try It: Attribute Presence
Let's use the presence selector to find all elements that have a title attribute and display their tag names.
<p title="Hello Tooltip">This paragraph has a title.</p>
<div>Another element</div>
<span title="Span Title">A span with a title.</span>
<div id="output"></div>
<script>
$(document).ready(function() {
$('[title]').each(function(index, element) {
$('#output').append('<p>Found: <' + $(element).prop('tagName').toLowerCase() + '> with title="' + $(element).attr('title') + '"</p>');
});
});
</script>Matching Exact Attribute Values
To select elements where an attribute has an exact value, use [attributeName="value"].
This selector is precise! It will only match if the attribute's value is identical to what you specify, including case (jQuery attribute value matching is case-sensitive).
Exact Match in Action
Find elements with a data-type attribute that is exactly "primary".
<button data-type="primary">Submit</button>
<button data-type="secondary">Cancel</button>
<a href="#" data-type="primary">Link</a>
<div id="output"></div>
<script>
$(document).ready(function() {
$('[data-type="primary"]').each(function(index, element) {
$('#output').append('<p>Exact match: <' + $(element).prop('tagName').toLowerCase() + '> (Type: ' + $(element).data('type') + ')</p>');
});
});
</script>Attribute Values That Start With...
The [attributeName^="value"] selector finds elements where an attribute's value begins with a specific string.
- It's useful for grouping elements with similar naming conventions.
- Great for filtering links (e.g., all links starting with
/products).
Ends With & Contains Values
These selectors offer more flexibility for partial matches:
[attributeName$="value"]: Matches if the attribute value ends with the specified string.[attributeName*="value"]: Matches if the attribute value contains the specified string anywhere within it.
Live Demo: Partial Matches
Observe how ^=, $=, and *= target different elements based on partial attribute values in their href attributes.
<a href="/products/item-1">Item 1</a>
<a href="/about/contact">Contact</a>
<a href="/products/item-2">Item 2</a>
<div id="output"></div>
<script>
$(document).ready(function() {
$('#output').append('<h3>Starts with "/products":</h3>');
$('[href^="/products"]').each(function() {
$('#output').append('<p>' + $(this).text() + '</p>');
});
$('#output').append('<h3>Ends with "contact":</h3>');
$('[href$="contact"]').each(function() {
$('#output').append('<p>' + $(this).text() + '</p>');
});
$('#output').append('<h3>Contains "item":</h3>');
$('[href*="item"]').each(function() {
$('#output').append('<p>' + $(this).text() + '</p>');
});
});
</script>Word and Hyphen Selectors
Two more specialized selectors:
[attributeName~="value"]: Matches if the attribute value contains a specific word, delimited by spaces. Often used withclassattributes.[attributeName|="value"]: Matches if the attribute value is exactly"value", or starts with"value-"(followed by a hyphen). Commonly used for language codes likelang="en-US".
Quick Check: Selectors
Which jQuery selector(s) would correctly identify the <div> element below?
<div class="card active-item" data-role="admin-panel" lang="en-US">
Admin Area
</div>Recap: Attribute Selectors
Great job! You've mastered advanced attribute selectors:
[attr]: Checks for attribute presence.[attr="val"]: Matches exact values.[attr^="val"]: Matches values starting with.[attr$="val"]: Matches values ending with.[attr*="val"]: Matches values containing.[attr~="val"]: Matches values containing a space-separated word.[attr|="val"]: Matches values exactly or starting with 'val-'.
Use these to write highly precise and efficient jQuery code!
Frequently asked questions
Is the “Advanced Attribute Selectors” lesson free?
Yes — the full text of “Advanced Attribute Selectors” is free to read here on the web, and the jQuery Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the jQuery Academy course, upgrade to CoddyKit PRO.
What will I learn in “Advanced Attribute Selectors”?
Explore complex attribute selectors to target elements based on their attributes' presence, value, or partial matches, enhancing your selection precision. You practise jQuery Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start jQuery Academy?
No prior experience is required. jQuery Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Advanced Attribute Selectors” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this jQuery Academy lesson?
Yes. Every jQuery Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Advanced Attribute Selectors
- Pseudo-Class and Custom Selectors
- Efficient DOM Traversal Techniques