I'm trying to display a number (1, 2, or 3) in my html file when I click a button.
Here is what I have so far:
<tr>
<td><button onclick="setDay(1)">1</button></td>
<td><button onclick="setDay(2)">2</button></td>
<td><button onclick="setDay(3)">3</button></td>
</tr>
<script type="text/javascript">
function setDay(a) {
var day = a
}
document.write(day)
</script>
I want to have it so when I click "button '1' ", for example, the function setDay() runs, with my argument set to 1. This way, the variable "day" is set to 1, and then when the variable 'day' is printed the number 1 is displayed. Any help would be greatly appreciated.
var daywill make the variable scoped to that function, it is not visible/usable outside it and since thewritecall is outside the function call it won't update each button click. UseinnerText,innerHTML, ortextContentof the element you want to change instead of document.write