0

Im trying to get my Array Entires inside a jQuery-Code which is inside a for-loop.

var list = ["name1", "name2" ];

for (var i = 0; i < list.length; i++)

{

$('ul.list li a[href*=(ary[i])]').parent().hide();

}

Im stuck and need help, thanks in advance!

BTW: This code is for a Greasemonkey Userscript (Firefox).

$('ul.list li a[href*="name"]').parent().hide();

works just fine. But with a couple hundret "names" I dont want to repeat this line all the time like this

$('ul.list li a[href*="name1"]').parent().hide();
$('ul.list li a[href*="name2"]').parent().hide();
[...]
$('ul.list li a[href*="name492"]').parent().hide();

EDIT: To avoid misunderstanding, in action the array would be not ["name1, "name2"] but something random like ["red", "banana", "pacific"]

2
  • 2
    Do these typos exist in your live code? list.lenght should be list.length? Do you get any errors in your javascript console? [href*=(ary[i])] won't work = needs to be [href="' + list[i] + '"] Commented Apr 2, 2014 at 22:20
  • length in my live code yes. but thanks for the href correction. a[href*="' + list[i] + '"] works fine! Thank you very much! Commented Apr 2, 2014 at 22:30

2 Answers 2

2
var list = ["name1", "name2" ];

$.each(list, function(index, name) {
    $('ul.list li a[href*="'+name+'"]').parent().hide();
}):
Sign up to request clarification or add additional context in comments.

Comments

0

A string literal is not evaluated for variable names. You need to concatenate the value with strings to use it that way:

$('ul.list li a[href*=' + list[i] + ']').parent().hide();

1 Comment

@cale_b: Thanks, didn't spot that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.