one
  • tow
  • here...">

    Search jQuery in an array of objects

    Suppose I have this html:

    <ul> <li class="cls">one</li> <li class="cls active">tow</li> <li class="cls">here</li> <li class="cls">after</li> </ul> 

    I select all .cls this jquery: $('.cls') selector and put them in a variable:

     var c=$('.cls'); 

    c is an array of an object. I want to select the .active element in this array using jQuery methods. I know I can use $('.cls.active') , but that is not what I am looking for. I want to use c . Is there any solution?

    note: c.find('.active') does not work because .find() searches on children.

    +6
    source share
    4 answers

    use filter () instead of find ()

    to find:

    Get the children of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

    filter:

    Reduce the set of matched elements to those that match the selector or perform a function check.

     c.filter('.active') 
    +3
    source

    Just use the filter() method for the jQuery object:

     var active = c.filter('.active'); 

    JS Fiddle proof of concept .

    Link:

    +1
    source

    Use . filter ()

     c.filter('.active'); 
    +1
    source

    Use .filter ()

    c.filter ('active.');

    0
    source

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


    All Articles