Reputation: 507
can anyone assist in locating the anchor tag via jQuery which contains a matching data-attribute please and then applying a class to it
e.g. code
<a href="#" data-label="test1">Link 1</a>
<a href="#" data-label="test2">Link 2</a>
<a href="#" data-label="test3">Link 3</a>
So if I pass a function the value "test2" a class of highlight would be applied to that one?
Thanks
Upvotes: 0
Views: 33
Reputation: 59
<a href="#" data-label="test1">Link 1</a>
<a href="#" data-label="test2">Link 2</a>
<a href="#" data-label="test3">Link 3</a>
addclass('test2');
function addclass(x){
$('[data-label="'+ x +'"]').addClass('active');
}
.active{
color: red;
}
Upvotes: 0
Reputation: 115242
Use attribute equals selector to get the element based on the attribute value.
$('[data-label="test2"]').addClass('active')
.active {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" data-label="test1">Link 1</a>
<a href="#" data-label="test2">Link 2</a>
<a href="#" data-label="test3">Link 3</a>
Upvotes: 1