how to add an event for every element selected in javascript code example
Example 1: javscript insert element after another
function insertAfter(newElement, referenceElement) {
referenceElement.parentNode.insertBefore(newElement, referenceElement.nextSibling);
}
var newElement = document.createElement("div");
newElement.innerHTML = "my New Div Text";
var myCurrentElement= document.getElementById("myElementID");
insertAfter(newElement, myCurrentElement);
Example 2: how to add class to element on mouseclick with js
document.querySelector("button").onclick = function addNewClass() {
document.querySelector(".elementToChange").classList.add("newClass");
}