JavaScript and HTML (help!) making alerts appear at the click of a button. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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.

5th May 2018, 2:26 PM
PathFinder
PathFinder - avatar
3 Answers
+ 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"); } }
5th May 2018, 4:03 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
@ChaoticDawg Thanks a lot! I have been stuck on this a while, I appreciate it.
5th May 2018, 4:42 PM
PathFinder
PathFinder - avatar
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...)
5th May 2018, 2:32 PM
PathFinder
PathFinder - avatar