0

I have a javascript array:

var exclude = ["Santorum","Obama","Romney","Gingrich"];

I have html links:

<a href="" class="title">Santorum is seeking campaign funding</a>
<a href="" class="title">Clinton is stepping down as Secretary</a>
<a href="" class="title">Obama is seeking reelection</a>

I want to check whether any of the a-links have any one of the exclude-values in them, and if they do, then remove them. So the result would be:

<a href="" class="title">Clinton is stepping down as Secretary</a>

The other two would be removed as they possess words from the array. I've tried using jQuery.inArray but I can't seem to figure it out. Thx for your help.

2 Answers 2

5

Something like:

var exclude = ["Santorum","Obama","Romney","Gingrich"];

$('a.title').filter($.map(exclude, function (val) {
    return ':contains("' + val + '")';
}).join()).remove();

http://jsfiddle.net/SxP2a/

Sign up to request clarification or add additional context in comments.

2 Comments

thx, it works. what if I want to remove a div that encompasses the a-link?
@Tomi then just add .closest('div') before remove()
2

Try looping though the excluded words, and remove links that have them.

For example (using the :contains selector):

$.each(exclude, function(i, v){
    $('a.title:contains("'+v+'")').remove();
});

DEMO: http://jsfiddle.net/H6FPb/

EDIT: You said the <a> is wrapped in a <div>, then you can do:

$.each(exclude, function(i, v){
    $('a.title:contains("'+v+'")').parent('div').remove();
});

2 Comments

thx, it works. what if I want to remove a div that encompasses the a-link?
@TomiSeus: Then you'd do .parent('div').remove()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.