Search code examples
jqueryhrefaddclassremoveclasshashref

removeclass from href contains youtube facebook but all href class is removed?


thanx in advance for replies

trying to addclass() for all href's with <a> tag

and want to exclude href contains example1.com , example2.com , example3.com

but i have tried a lot with

var exclude = ["example1.com","example2.com","example3.com"];

$('a.class').has('a[href='"+exclude+"']').removeClass('class');

or

$('a.class').has('a:contains('"+exclude+"')').removeClass('class');

but all class removed

hope to help me


Solution

  • You can use the array elements to create the selector for targeting elements :

    $("a[href*='" + exclude.join("'],[href*='") + "']").removeClass('class');
    

    Working Demo

    You do not need to perform both addClass and removeClass. You can use above selector along with .not() to filter them out:

    $('a').not($("[href*='" + exclude.join("'],[href*='") + "']")).addClass('class');
    

    Demo