Selecting Elements Based on Attributes - J Query

Problem
You need to select elements based on attributes and those attributes’ values.

Solution

Use an attribute selector to match specific attributes and corresponding values:

The preceding selector would select all anchor elements with an href attribute equal
to the value specified (http://google.com).
There are a number of ways you can make use of the attribute selector:

[attr]
Matches elements that have the specified attribute
[attr=val]
Matches elements that have the specified attribute with a certain value
[attr!=val]
Matches elements that don’t have the specified attribute or value
[attr^=val]
Matches elements with the specified attribute and that start with a certain value
[attr$=val]
Matches elements that have the specified attribute and that end with a certain value
[attr~=val]
Matches elements that contain the specified value with spaces, on either side (i.e.,
car matches car but not cart)
You can also combine multiple attribute selectors:

Discussion

As always, for special requirements it may be more suitable to use the filter() method to more specifically outline what you’re looking for:

In this filter, a regular expression is being used to test the href attribute of each anchor.
It selects all external links within any page.
The attribute selector is especially useful for selecting elements based on slightly varying
attributes. For example, if we had the following HTML:

we could use the following selector to match all of the <div>elements:


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

J Query Topics