Reputation: 95
I am trying to create a new p
tag after the button
is clicked yet nothing happens after it's clicked:
(function() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 403) {
var heading = document.getElementById("head");
var para = document.createElement("P");
para.innerHTML = "Accept the T&Cs to Continue";
var button = document.createElement("BUTTON"); // button created here
button.classList.add("theButton");
var theButton = document.getElementsByClassName("theButton");
theButton.onclick = function() { // button doesn't work onclick
var newP = document.createElement("P");
newP.innerHTML = "DONE";
heading.appendChild(newP);
heading.appendChild(para);
heading.appendChild(button);
};
}
};
xhttp.open("GET", "/content.ajax", true);
xhttp.send();
})();
Can someone help me with this?
Upvotes: 0
Views: 45
Reputation: 103
I´m assuming that document.getElementsByClassName("theButton") should get your button, because it uses the same class.
Also you should consider using addEventListener because your listener can´t be overwritten that way and you can add multiple listeners.
(function () {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if(this.readyState ==4 && this.status == 403){
var heading = document.getElementById("head");
var para = document.createElement("P");
para.innerHTML = "Accept the T&Cs to Continue";
var button = document.createElement("BUTTON"); // button created here
button.classList.add("theButton");
button.addEventListener("click", function() {
var newP = document.createElement("P");
newP.innerHTML = "DONE";
heading.appendChild(newP);
});
heading.appendChild(para);
heading.appendChild(button);
}
};
xhttp.open("GET","/content.ajax",true);
xhttp.send();
})();
Upvotes: 1