Monday, 13 June 2016

Add class depending href of anchor

Add class depending href of anchor from current url



This jquery will add class active to anchor tag having that href

ex. url is www.test.com#tag

var hash = jQuery(this).attr('href').split("#");  //  it will give tag as result

jQuery(('a[href$="#'+hash[1]+'"]')).parent().addClass('active');

// this will add class to that a which have #tag as its href

Note: you can use these also


= is exactly equal
!= is not equal
^= is starts with
$= is ends with
*= is contains
~= is contains word
|= is starts with prefix (i.e., |= "prefix" matches "prefix-...")


banner
Previous Post
Next Post

0 comments: