search

Home  >  Q&A  >  body text

How to assign variable to html input

I want to assign a variable to the html input. This is what I tried but it doesn't work.

<input type=number>let x = 10</input>

I want to assign a variable to the html input so that I can use it later, how can I do this.

P粉008829791P粉008829791340 days ago604

reply all(1)I'll reply

  • P粉302160436

    P粉3021604362024-02-05 00:34:02

    Get the input element in js and assign it a value like this

    const input = document.querySelector("input");
    let x = 10;
    input.value = x;

    reply
    0
  • Cancelreply