0
JavaScript and HTML (help!) making alerts appear at the click of a button.
So I have 3 buttons on screen and I want each one when clicked to display an alert box when clicked however only the bottom box works. What am I doing wrong? I will post the code in the comments as there isn’t enough room to do so here.
3 Respostas
+ 3
1st in your HTML the input tag isn't closed. It's missing >.
window.onload = function() {
let x = document.getElementById("b1");
let y = document.getElementById("b2");
let z = document.getElementById("b3");
x.onclick = function() {
alert("Button 1");
}
y.onclick = function() {
alert("Button 2");
}
z.onclick = function() {
alert("Button 3");
}
}
+ 1
@ChaoticDawg
Thanks a lot!
I have been stuck on this a while, I appreciate it.
0
window.onload = function() {
var x = document.getElementById("b1");
x.onclick = function(){alert("Button 1");}
}
window.onload = function() {
var x = document.getElementById("b2");
x.onclick = function(){alert("Button 2");}
}
window.onload = function() {
var x = document.getElementById("b3");
x.onclick = function(){alert("Button 3”);}
}
*HTML CODE*
<form>
<input id=“b1” type=“button” value=“Button 1”
</form>
(Same code for button 2 and 3 etc...)