Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery search in array of object

Lets say 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'm selecting all .cls by this jquery selector : $('.cls') and put them in a variable:

var c=$('.cls');

c is an array of object now. I want to select .active item in this array by jQuery methods. I do know i can use $('.cls.active') but this is not what I'm lookin for. I want to use c. Is there any solution ?

note: c.find('.active') not working, because .find() searching in childs.

like image 657
Omid Avatar asked Aug 31 '13 08:08

Omid


People also ask

How do you check if an array contains an item?

JavaScript Array includes() The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found. The includes() method is case sensitive.

How do you check if an array contains a value in jQuery?

1) Using jQuery If you are someone strongly committed to using the jQuery library, you can use the . inArray( ) method. If the function finds the value, it returns the index position of the value and -1 if it doesn't.


1 Answers

use filter() instead of find()

find:

Get the descendants 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 pass the function's test.

c.filter('.active')
like image 128
Arun P Johny Avatar answered Sep 19 '22 20:09

Arun P Johny