Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
77
rated 0 times [  79] [ 2]  / answers: 1 / hits: 11890  / 10 Years ago, wed, may 28, 2014, 12:00:00

Trying to find a selector to get all elements that have both no class, and no id set to them.



So far I have 2 different outputs depending on if there is a space in the selector:



                                      // outputs
var noID = $('*:not([id])');// 144 - may have a class
var noClass = $('*:not([class])'); // 100 - may have an id

var withSpace = $('*:not([id]) *:not([class])'); // 99 ?
var noSpace= $('*:not([id])*:not([class])'); // 84 ?


Which one is correct, my guess is the noSpace - but I don't know. Anyone tried this before?



My guess is that with the space, the selector is going inside the tag that has no ID, and selecting the children elements that have no class associated with them.



And the noSpace result is the correct one, as it selects only the elemets that have both no class, and no id.



Can someone verify? Thanks!






Answer



Use this selector to find elements that have neither a class, or an id associated with them.



$('*:not([id]):not([class])');


bonus: $('body *:not([id]):not([class])'); - If you only want to deal with the actual content


More From » jquery

 Answers
26

withSpace - $('*:not([id]) *:not([class])'); will find all elements with no class that are inside an element without an ID. Putting a space in the selector is like calling find seperately.



You could change noSpace to be this instead and still get the right result:



var noSpace= $('*:not([id]):not([class])'); // second * not needed


JSFiddle used for testing


[#44977] Tuesday, May 27, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brittanye

Total Points: 263
Total Questions: 94
Total Answers: 115

Location: Burkina Faso
Member since Thu, Dec 23, 2021
3 Years ago
brittanye questions
Mon, Aug 10, 20, 00:00, 4 Years ago
Tue, Jun 16, 20, 00:00, 4 Years ago
Wed, Apr 22, 20, 00:00, 4 Years ago
Mon, Apr 13, 20, 00:00, 4 Years ago
;