-2

Good day. So sorry if this was asked before, but in that case I am unsure of what to ask to get the answer - so do link me up if so. What I want to do is simple, take an input field's data, assign it to a variable in javascript. To please not be done with inline functions, only dynamically from javascript calling the queryselector.

Thanks in advance.

Ankhi
  • 1
  • Nah, even though your solution worked once - it stopped working right after. And no matter how many times I clear the console, it refuses to return nothing but an empty variable. It stopped working after I added a second input field, assigning it the same function. Almost worked - thanks though! – Ankhi Mar 04 '21 at 09:01

1 Answers1

0
  const inputField = document.querySelector("#inputId");
  let value = inputField.value;

  console.log("value from input:", value);

  // if you want to update variable dynamically after changing input value
  inputField.addEventListener("keydown", (e) => {
    value = e.target.value;
    console.log("updated value from input:", value);
  });