I have a problem with very basic js function. I've looked around and found this topic on SO which is very similar but it didn't help me.
So I want to run a function after the div is clicked. My first attempt would cause the function to run without clicking it:
1st attempt
document.getElementsByClassName('button').addEventListener("click", slide());
function slide(){
x = document.getElementsByClassName('button')[0];
x.innerHTML = "test123";
}
.button{
height:60px;
background-color:darkred;
}
<div class="button" id="button"></div>
Then after adjusting my code to what I read in the answer provided in the topic I linked above I created something like this:
2nd attempt
document.getElementsByClassName('button').addEventListener("click", function(){
slide();
});
function slide(){
x = document.getElementsByClassName('button')[0];
x.innerHTML = "test123";
}
.button{
height:60px;
background-color:darkred;
}
<div class="button" id="button"></div>
But then nothing happens at all after clicking the div
. I also tried to do it like this:
document.getElementsByClassName('button').addEventListener("click", slide);
But then also nothing happens. Can someone explain me what I am doing wrong? Thank you in advance.