Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript add class to all child elements

// Add class 'hide' to every child div on 1st child lvl 
var el = document.getElementById('your-element-id');
var classList = 'classList' in el;
for (var i = 0; i < el.children.length; i++) {
  var child = el.children[i];
  if (child.tagName == 'DIV') {
    if (classList) {      
        child.classList.add('hide');                                  
    } else {
      child.className += ' hide'
    }}}  
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #add #class #child #elements
ADD COMMENT
Topic
Name
8+4 =