score:1

Accepted answer

You simply need to bind count to the value of the input. REPL

<script>
  let count = 0;
  
  const handleClick = () => count++;
  
  const handleChange = (e) => {
    const userValue = e.target.value;
    const newValue = userValue ? parseInt(userValue, 10) : 0;
  
    count = isNaN(newValue) ? count : newValue;
  };    
</script>

Count: <button on:click={handleClick}>{count}</button>
<br />
A: <input bind:value={count} on:input={handleChange} />
<br />

Note: Remember to always pass a radix to parseInt(), it does not default to 10.


Related Query

More Query from same tag