Index Page
  • Some Pag...">

    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
    source share
    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'); } }); 

    See attribute equals selector

    +9
    source

    prop () returns if the property is set or its value, which means true or false or a string - and there is no is() method in a Boolean or string.

    +2
    source

    Source: https://habr.com/ru/post/1390627/


    All Articles