js get child by class code example

Example 1: get child element by class javascript

var element = document.querySelector('#Element');
var elementChildren = element.querySelectorAll('.Element-child');

Example 2: javascript get child by name

let myForm = document.getElementById('form');

let child = myForm.querySelector('input[name="username"]');

console.log(child);

Example 3: 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 4: javascript get child element by class

// get child element of #test with class my-class
var testContainer = document.querySelector('#test');
var childNodeByClass = testContainer.querySelector('.my-class');