warmjaijai
warmjaijai

Reputation: 1011

JS specify HTML elements with its attribute

I would like to refer to the elements by specifying its multiple attribute

i can do it with only one attribute but i failed to research anything regarding to multiple attribute. Not sure where I should start from

var l_id = $(this).get(0).getAttribute('data-href');
$('a[data-href="'+l_id+'"]').css('color', '#C99A0C');

The above works as fine but my tag a also has another attribute called data-type, i was doing something like below but failed

var l_id = $(this).get(0).getAttribute('data-href');
var l_type = $(this).get(0).getAttribute('data-type');
$('a[data-href="'+l_id+'" data-type="'+l_type+'"]').css('color', '#C99A0C');

Please help, thanks.

Upvotes: 1

Views: 24

Answers (1)

Jonathan
Jonathan

Reputation: 6537

To use multiple attributes for one element, this will select all <a>'s with both your data-href AND your data-type.

$('a[data-href="'+l_id+'"][data-type="'+l_type+'"]').css('color', '#C99A0C');

If you are trying to get elements that match either one or the other instead, this will match elements that have the data-href as well as elements with the data-type.

$('a[data-href="'+l_id+'"],a[data-type="'+l_type+'"]').css('color', '#C99A0C');

Upvotes: 2

Related Questions