select child with class javascript code example
Example 1: get child element by class javascript
var element = document.querySelector('#Element');
var elementChildren = element.querySelectorAll('.Element-child');
Example 2: get all the child of the same class javascript
var doc = document.getElementById("test");
var notes = null;
for (var i = 0; i < doc.childNodes.length; i++) {
if (doc.childNodes[i].className == "4") {
notes = doc.childNodes[i];
break;
}
}
Example 3: javascript get child element by class
var testContainer = document.querySelector('#test');
var childNodeByClass = testContainer.querySelector('.my-class');
Example 4: select child javascript
Syntax
element.children
Example
Change the background color of the second child element of a <div> element:
var c = document.getElementById("myDIV").children;
c[1].style.backgroundColor = "yellow";