JQuery `is` is not a function
I have the following HTML menu:
<ul> <li><a href="index.html">Index Page</a></li> <li><a href="#some-page">Some Page</a></li> </ul> Let's say that var url = "some-page";
$('#menu li a').prop('href') .is('#' + url ) .parrent() .addClass('active'); I get an msg .is is not a function error .is is not a function error. What for? .is is obviously a function.
Any suggestion is much appreciated.
+4
2 answers
The prop() method that you call before is() returns 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'); Remember that is() returns true or false if any of the matched elements matches the selector; therefore, you can use the following form if you want to use is() ;
$('#menu li a').each(function () { if ($(this).is('[href="#' + url +'"]')) { $(this).parent().addClass('active'); } }); +9