Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery `is` is not a function

Tags:

jquery

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.

like image 242
Iladarsda Avatar asked Dec 21 '22 03:12

Iladarsda


1 Answers

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

like image 136
Matt Avatar answered Jan 02 '23 02:01

Matt