You have a number of problems; so we'll start looking at the basics from the JavaScript you posted:
document.getElementByClassName("thisclass")
// ^- typo, the function is: getElementsByClassName
// (note the additional 's')
.addEventListener("click",function(){
// a NodeList - returned by the getElementsByCLassName()
// function has no addEventListener() method, so instead
// you have to iterate over all elements in the NodeList
// and apply the event-listener to each element individually.
//do something with the specific clicked div
});
So the above would be correctly written as:
Array.from(document.getElementsByClassName('thisclass')).forEach(
currentNode => currentNode.addEventListener('click', function(){
// do whatever
});
Or slightly more succinctly:
document.querySelectorAll('.thisclass').forEach(
currentNode => currentNode.addEventListener('click', function(){
// do whatever
});
Which takes advantage of the NodeList returned by document.querySelectorAll()
having a forEach()
method, whereas the returned Live NodeList returned by document.getElementsByClassName()
does not.
The other part of the problem is that you seem to want to add the event-listeners to those elements that do not yet exist in the document.
Because JavaScript can't directly bind event-listeners to elements that are added to the document after the event-listeners are bound, we instead have to add the event-listener to an existing ancestor element on the page rather than directly to the elements themselves.
As a simple example, making use of an added <button>
element to add new elements:
// caching references to various elements for later use:
let button = document.querySelector('button'),
ancestor = document.body;
// named function to add new elements to the page;
// the first argument is the 'event' Object,
// passed automagically from the addEventListener()
// method:
function addNewElement(event) {
// because we're using the ancestor element of
// the <button> to listen for the same 'click'
// event, here we use event.stopPropagation()
// to avoid that click bubbling up through the
// document:
event.stopPropagation();
// we find the first element matching the supplied
// selector (or if no such element exists this
// will return null; so in production it's sensible
// to check the element exists):
let newElement = document.querySelector('.thisclass')
// and then we clone that Node:
.cloneNode(true);
// this is passed automagically from the
// addEventListener() method, and refers to
// the element on which the event-listener
// was fired; here we find the <button>
// element's parentNode and use the
// parentNode.appendChild() method to add
// the newly-created element to the document:
this.parentNode.appendChild(newElement);
// here we explicitly add a class to the
// dynamically added content, just to be
// explicit that it's a newly-added element:
newElement.classList.add('dynamicallyAdded');
}
// a simple named function to demonstrate
// simple functionality:
function handleThisclassClicks(event) {
// the event.target is the Node upon which
// the listened-for event was originally
// fired:
let target = event.target;
// here we simply toggle the 'active'
// class on the clicked element:
target.classList.toggle('active');
}
// binding the named ('addNewElement()') function
// as the event-handler for the click event on
// the button Node (note the deliberate lack of
// parentheses):
button.addEventListener('click', addNewElement);
// binding the named function as the event-handler for
// the click event fired on the ancestor Node (which is,
// again, why we used event.stopPropagation() in the
// button Node's click-handler):
ancestor.addEventListener('click', handleThisclassClicks);
let button = document.querySelector('button'),
ancestor = document.body;
function addNewElement(event) {
event.stopPropagation();
let newElement = document.querySelector('.thisclass')
.cloneNode(true);
this.parentNode.appendChild(newElement);
newElement.classList.add('dynamicallyAdded');
}
function handleThisclassClicks(event) {
let target = event.target;
target.classList.toggle('active');
}
button.addEventListener('click', addNewElement);
ancestor.addEventListener('click', handleThisclassClicks);
div {
color: red;
margin-left: 0;
transition: color 0.5s linear, margin-left 1s linear;
}
div.active {
color: limegreen;
margin-left: 1em;
}
.dynamicallyAdded::after {
content: '*';
}
<button>Add another '.thisclass' element</button>
<div class="thisclass">
HTML
</div>
<div class="thisclass">
HTML
</div>
<div class="thisclass">
HTML
</div>
References:
Bibliography:
‹
document.getElementByClassName("settings-opslag").addEventListener("click",function(){ document.getElementByClassName("settings-opslag").style.background = "red"; }); – Jul 10 '17 at 13:17