Sylvain Attoumani
Sylvain Attoumani

Reputation: 1194

Get value from input type text

I'm working with javascript and try to do a simple thing. I want to get the value from an input text as you type. But can't figure out why it's not working

function myFunction() {
  console.log('ok')
  let input = document.getElementById('test')
  console.log(input)
  console.log('2' + input.value)
}
<input class="recherche_field" onkeyup="myFunction()" type="text" id="test" name="search_query" placeholder="Ici" />

and no matter what I write in my input my console show that :

ok
<input class=​"recherche_field" onkeyup=​"myFunction()​" type=​"text" id=​"test" name=​"search_query" placeholder=​"Ici">​
2

Upvotes: 0

Views: 70

Answers (2)

Mayur Narula
Mayur Narula

Reputation: 150

The code written by you seems fine.

function myFunction() {
  console.log('Key Up Event Triggered')
  var input = document.getElementById('textBox')
  console.log(input)
  console.log('2' + input.value)
  console.log('Key Up Event Performed')
}
<input id="textBox" type="text" placeholder="Insert Text" onkeyup="myFunction()" />    

Upvotes: 0

samanime
samanime

Reputation: 26537

Code-wise, what you have should work.

function myFunc() {
  console.log('keyup');
  const input = document.getElementById("test");
  console.log("2" + input.value);
}
<input id="test" onkeyup="myFunc()" />

The most likely cause of your issue is having multiple inputs on the page with an id of "test". Every HTML element on the page should have a unique ID.

Upvotes: 1

Related Questions