How do I make a button that adds one to a display every time it is clicked? (in HTML or Javascript please?)
I probably should clarify "display". I mean like a number that appears somewhere on the screen.
Is this possible?
You can start with something like the following:
let count = 0; button.addEventListener("click", () => { result.textContent = count + 1; count++; });
<div id="result">0</div> <button id="button">Add 1</button>
In order to make that work in the browser, create an HTML file, so like index.html
and add the following content to it:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="result">0</div>
<button id="button">Add 1</button>
<script>
let count = 0;
button.addEventListener("click", () => {
result.textContent = count + 1;
count++;
});
</script>
</body>
</html>
Then you can open that file with the browser and it should be a good place to get started.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.