i have a html tag as below (no id
or class
defined):
<a href="#" letter="xyz">All</a>
i need to get the tag having attribute value='xyz'
i was successful in finding the attribute by:
var html_tag = $("a").find('[letter="xyz"]');
but it returns an object.
i need to get html tag so that i can use it as:
$(html_tag).css('color','#FFFFFF');
Or suggest me any other solution if this is not possible.

- 171
- 1
- 9
-
Are you saying that _any_ element might have that attribute, so in this instance the result of `html_tag` should just be `a`, so you can then select _all_ `` tags? – James Thorpe Sep 16 '15 at 11:17
-
Nopes.. whats i was trying to achieve was answered by Satpal correctly :) i was having list of `` having "unique" attribute (letter) value. So i needed to find that specific tag that contains attribute having value equal to "xyz" – Danish Ahmed Sep 16 '15 at 11:25
3 Answers
$("a").find('[letter="xyz"]')
will find descendants of anchor with attribute value letter="xyz"
You need to use Attribute Equals Selector [name=”value”] with anchor tag
Selects elements that have the specified attribute with a value exactly equal to a certain value.
Use
$('a[letter="xyz"]').css('color','#FFFFFF');
You could also use .filter()
Reduce the set of matched elements to those that match the selector or pass the function's test.
$("a").filter('[letter="xyz"]').css('color','#FFFFFF');

- 132,252
- 13
- 159
- 168
Your code is trying to find element in anchor element that have letter attr equal to xyz. which do not exists. Using .filter()
instead of .find()
would solve the problem.
However you can simply use:
var html_tag = $('a[letter="xyz"]');
Note: you should use .data-*
attributes(added in HTML5) for adding custom attributes as adding your own attributes can break elements validation and make html invalid (see https://stackoverflow.com/a/1735239/1719752).

- 1
- 1

- 81,290
- 25
- 94
- 125