let counter = parseInt(document.getElementById("#integer").innerHTML)
let incbutton = document.querySelector("#increase")
incbutton.addEventListener("click", increase)
function increase() {
counter += 1;
}
<h1 id="integer">100</h1>
<button id="increase">Increase</button>
when ı try to convert to integer content of h1 tag increase button is not working. ı think because of h1 tag still string not integer. what is the mistake here? your text
2
Answers
simply update the counter value
The following code converts the content of the element with the ID
integer
to an integer:If the content of the element is not a valid integer, the parseInt() function will return NaN.
To update the content of the element with the ID
integer
with the new value of the counter, you can use theinnerHTML
property:Here is a complete example of your code