Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value from input with onclick

Tags:

javascript

How can I get the value of input on click using vanilla javascript?

function getvalue() {
  console.log(this.val);
}
<input type="text" onclick="getvalue()" value="asdf"></input>
<input type="text" onclick="getvalue()" value="asdf2"></input>
<input type="text" onclick="getvalue()" value="asdf3"></input>
like image 948
Ivan Topić Avatar asked Jan 04 '23 13:01

Ivan Topić


1 Answers

Use event.target.value inside your function call

When function gets called event object is passed to the function. event.target identifies which element called the function.

function getvalue() {
   console.log(event.target.value);
}
<input type="text" onclick="getvalue()" value="asdf"></input>
<input type="text" onclick="getvalue()" value="asdf2"></input>
<input type="text" onclick="getvalue()" value="asdf3"></input>
like image 179
Rikin Avatar answered Jan 15 '23 06:01

Rikin