I have following HTML menu:
<ul>
<li><a href="index.html">Index Page</a></li>
<li><a href="#some-page">Some Page</a></li>
</ul>
Let say that var url = "some-page";
$('#menu li a').prop('href')
.is('#' + url )
.parrent()
.addClass('active');
I'm keep getting error msg .is is not a function. Why? .is is clearly a function.
Any suggestion much appreciated.
The prop() method you are calling before is() is returning the href property as a string.
Strings do not have an is() method.
Try;
$('#menu li a').filter('[href="' + url + '"]').parent().addClass('active');
Although you could combine this into one selector;
$('#menu li a[href="' + url + '"]').parent().addClass('active');
Be aware that is() returns true or false if any of the matched elements matches the selector; so the following form could be used if you wanted to use is();
$('#menu li a').each(function () {
if ($(this).is('[href="#' + url +'"]')) {
$(this).parent().addClass('active');
}
});
See the attribute equals selector
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With