JQuery - select a subset of elements in a saved set of elements

From this:

var $saved = $('#parent').find('a'); 

How can I now sub-select those elements in $saved that have the myClass class?

I don’t want the descendants (therefore, find or children do not fit), I want a subset of $ saved.

 var $refined = $saved.[something something]; 

Essentially, I want $refined to be $('#parent').find('a.myClass'); but started with $saved .

Thanks.

+6
source share
5 answers

You can use the filtering method:

 var $refined = $saved.filter(".myClass"); 
+14
source

You can use the filter method.

 var $refined = $saved.filter('.myClass'); 
+4
source

You can iterate through a saved collection to find out the elements with the myClass class.

 var $refined = $saved.each(function(){ if($(this).attr('class') == 'myClass') return $(this); }); 

Or you can use the filter () jquery function to apply a selector.

  var $refined = $saved.filter('myClass'); 
+2
source

Maybe this will help

 var $refined = $saved.filter('.myClass'); 

http://api.jquery.com/filter/

+2
source
 $saved.each(function(){ if($(this).hasClass('test')) alert($(this).text()); }); 
+1
source

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


All Articles