So I have 4 buttons, when I click either or button, it should display the number that I've clicked with "Result = (the button number)." If I try creating else statements it still doesn't work. How would I make it so that if I click a specific button, that button number would be displayed
My HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title> Task 2 </title>
<link href="style.css" type="text/css" rel="stylesheet">
<script src="task2.js" type="text/javascript"></script>
</head>
<body>
<!-- Create a paragraph with id mydata -->
<div id="box">
<p style="font-family: monospace;" id="mydata">Result </p>
<!-- Create 4 Click buttons -->
<!-- Number 10 -->
<p> <button onclick="myFunction();"> 10 </button></p>
<!-- Number 20 -->
<p> <button id="twenty" onclick="myFunction();"> 20 </button></p>
<!-- Number 30 -->
<p> <button id="thirty" onclick="myFunction();"> 30 </button></p>
<!-- Number 40 -->
<p> <button id="fourty" onclick="myFunction();"> 40 </button></p>
</div>
</body>
</html>
And my JS
var num1 = 10
var num2 = 20
var num3 = 30
var num4 = 40
function myFunction()
{
var p = document.getElementById("mydata"); // get the paragraph
if (num1 == 10)
{
p.innerHTML = "Result = " + num1;
}
}