Reputation: 518
I need to compare a var foo = 'whatever'
with THIS:
<ul id="nav_main"><li><a href="index.php?pag=THIS">link</a></li></ul>
And then, put class="current"
inside the matched <li>
.
Upvotes: 0
Views: 439
Reputation:
if (foo == "<ul id="nav_main"><li><a href="index.php?pag=THIS">link</a></li></ul>"){
foo.addClass("current")
}
Upvotes: 0
Reputation: 227310
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>
.
Upvotes: 1
Reputation: 14219
$('#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
}
});
Upvotes: 0
Reputation: 78590
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
Upvotes: 5