I have created a simple js code:
var testContainer = document.getElementById("test");
var butt = document.getElementById("test-button");
butt.addEventListener("click", function(){
var paragraph = document.createElement("p");
paragraph.innerText = "test";
testContainer.appendChild(paragraph);
});
With HTML code:
<form>
<div id="test"></div>
<button id="test-button"> test </button>
</form>
And here is the JSFiddle.
My question is why when <button>
is inside <form>
then my event listener is blocked and it cannot add a new element to <div id="test">
?
I don't understand why it's not valid.