javascript - How to make sure all elements contain a specific class in jQuery? -


i need check make sure elements children of parent container have specific class. can tell me how in jquery?

if paragraph tags of div tag have class "correct"

-> yep, of them have class

otherwise, not paragragh tags in div tag have class

-> noope, not have required class

if mean direct children, then:

if ($('div').children('p').length === $('div').children('p.correct').length) {   // yes } else {   // no } 

to include descendant <p> tags in reckoning, use ".find()" instead of ".children()".

edit — it'd keen have plugin stuff this:

$.fn.all = function(pred) {   var rv = true;   this.each(function(element) {     if (!pred(element)) return (rv = false);   });   return rv; }; 

then write:

if ($('div p').all(function(p) { return p.hasclass('correct'); })) {   // yes } 

a similar ".any()" function useful too.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -