I am making something like a clicker in Javascript and the "onclick" only works once unless I refresh the page.
My HTML code :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<div id="main">
<button id="o"> Click Me</button>
</div>
<script type="module" src="/main.js"></script>
</body>
</html>
JS code :
import './style.css';
let x = 0;
document.querySelector('#main').innerHTML = `
<button id="o">Click Me</button>
<p>${x}</p>
`;
document.querySelector("#o").onclick = function() {
x++;
console.log(x);
document.querySelector('#main').innerHTML = `
<button id="o">Click Me</button>
<p>${x}</p>
`;
};
When I open the console, I only see the log "1" when I first click the button, not afterwards. There is no CSS styling.
I am using Vite to make this.
` element in the initial markup?
– Andy Jun 29 '23 at 11:17