Refactoring a jquery selector expression

Is there a way that I can write the following selector in a less verbose manner (the parent id repeats over and over).

$("#parent_id .class1 , #parent_id .class2,....") 

Thanx!

+4
source share
3 answers

List your classes in the context of the object as follows:

 $('.class1, .class2, .class3', $('#parent_id')) 

Edit : jsFiddle example .

+5
source
 $("#parent_id").find(".class1, .class2, .class3")..... 
+8
source

I'm not sure if you understand you. You can select multiple elements using jQuery: http://api.jquery.com/multiple-selector/

So, something like $ (". Class1, .class2"); should work.

-one
source

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


All Articles