js queryselectorall code example
Example 1: js foreach querySelectorAll
const allSpanElements = document.querySelectorAll('span');
allSpanElements.forEach((spanElement) => {
// Here comes the Code that should be executed on every Element, e.g.
spanElement.innerHTML = "This Content will appear on every span Element now";
});
Example 2: js queryselectorall
var container = document.querySelector("#test");
var matches = container.querySelectorAll("div.highlighted > p");
Example 3: js queryselectorall
var matches = document.querySelectorAll("p");
Example 4: js querySelectorAll
// https://www.google.com/
// this puts a border around the 'a' tags of 'Google offered in' section.
// within this id find all the 'a' tags
var languages = document.querySelectorAll("#SIvCob a");
// loop through all the a tags a add a border to the tags
for(var i = 0; i < languages.length; i++){
document.querySelectorAll("#SIvCob a")[i].style.border = "1px solid black";
}
Example 5: js queryselectorall
var matches = document.querySelectorAll("div.note, div.alert");
Example 6: js queryselectorall
elementList = parentNode.querySelectorAll(selectors);