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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With