I am trying to create 3 buttons and use javascript, if button 1 is pressed, it changes the text "Press the button" to "You pressed the button 1"
The same goes for buttons 2 and 3! And if you press button 3, the text color will change to GREEN
However, I canβt get it to work! Any help would be appreciated
Here is my current code:
<!DOCTYPE html> <html> <head> <title>Button</title> </head> <body> <script type="text/javascript"> function changeText() { var b1 = document.getElementById('b1'); var b2 = document.getElementById('b2'); var b3 = document.getElementById('b3'); if(b1.onclick="changeText();") { document.getElementById('pText').innerHTML = "You pressed button 1"; } if(b2.onlick="changeText();") { document.getElementById('pText').innerHTML = "You pressed Button 2"; } } </script> <input type="button" value="Button 1" id="b1" onclick="changeText();"/> <input type="button" value="Button 2" id="b2" onclick="changeText();"/> <input type="button" value="Button 3" id="b3" onclick="changeText();"/> <p id="pText">Click on a Button</p> </body> </html>
javascript function html text onclick
Gdesigns
source share