Skip to content Skip to sidebar Skip to footer

Comparing A Javascript Variable With A Part Of A Href

I need to compare a var foo = 'whatever' with THIS: And then, put class='

Solution 1:

var foo = 'whatever';
$("#nav_main li a").filter(function(index){
    return foo === this.href.match(/pag=(.*)/)[1];
}).parent().addClass("current");

Would that work?

Demo: http://jsfiddle.net/rREry/1

Solution 2:

var foo = 'THIS';
$('a[href*="'+foo+'"]', '#nav_main').closest('li').addClass('current');

This searches for <a> tags that have an href attribute containing the string, and adds the class to their containing <li>.

Solution 3:

$('#nav_main li').each(function() {  // loop through each <li>if ($(this).find('a').attr('href') == foo) {   // check against your var (not sure what exactly you wanted to check)
        $(this).addClass('current');       // add the "current" class if it matches
    }
});

Solution 4:

if (foo == "<ulid="nav_main"><li><ahref="index.php?pag=THIS">link</a></li></ul>"){
  foo.addClass("current")
}

Post a Comment for "Comparing A Javascript Variable With A Part Of A Href"