Reputation: 51
I've searched for about an hour for this but could not find it. I'm trying to use jQuery to find and highlight an array of links on the page. What I originally had was:
$(table).find('a[href*="7000"], a[href*="7001"], a[href*="6020"], a[href*="6987"]').addClass('highlight');
Which is fine, until you have a really long list of links with different numbers to look for (which is what I'll have soon). Basically, I want to enter all the numbers in an array and use those. So I tried:
var $href = 'a[href*="';
var $numbs = ('7000', '7001', '6020', '6987');
var $end = '"]';
$('.tripname').find($href + $numbs + $end).each(function() {
$(this).addClass('highlight');
});
But it didn't work. It only finds the last one, apparently.
Any thoughts on this are appreciated.
Upvotes: 0
Views: 22
Reputation: 14927
Your array syntax is wrong. Arrays are surrounded by square brackets.
After fixing that, you could make use of:
Array.map
to generate the selector for each number, and Array.join
to combine them into a multiple selectorconst numbers = [7000, 7001, 6020, 6987];
const selector = numbers
.map(n => `a[href*="${n}"]`)
.join(',');
console.log(selector);
You can then use that selector with jQuery.
Upvotes: 2