Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the value of input text when enter key pressed

I am trying this:

<input type="text" placeholder="some text" class="search" onkeydown="search()"/> <input type="text" placeholder="some text" class="search" onkeydown="search()"/> 

with some javascript to check whether the enter key is pressed:

function search() {     if(event.keyCode == 13) {         alert("should get the innerHTML or text value here");     } } 

this works fine at the moment, but my question how to get the value inside the text field, I was thinking of passing a reference "this" to the function, or if they had id's then I could use ID's but then I don't see how I could differentiate between which one has been typed, bringing my back to the same problem again...

like image 450
user2405469 Avatar asked Jan 08 '14 14:01

user2405469


Video Answer


2 Answers

Try this:

<input type="text" placeholder="some text" class="search" onkeydown="search(this)"/>   <input type="text" placeholder="some text" class="search" onkeydown="search(this)"/> 

JS Code

function search(ele) {     if(event.key === 'Enter') {         alert(ele.value);             } } 

DEMO Link

like image 62
brg Avatar answered Sep 19 '22 23:09

brg


$("input").on("keydown",function search(e) {     if(e.keyCode == 13) {         alert($(this).val());     } }); 

jsFiddle example : http://jsfiddle.net/NH8K2/1/

like image 45
Alexander Avatar answered Sep 18 '22 23:09

Alexander