Tuesday, April 11, 2017

Capturing keypress and passing a value javascript

i have this on a template in my laravel application

 <input type="text" class="form-control" placeholder="Post a comment" onkeyup="handleEvt(this, , e)"/>

and i want a user to be able to post a comment by just hitting the enter key, after typing. i have the handleEvt() function with three params.

the first parameter - this, is used to get the input object using jquery $(this) and the second parameter - is for the posts id, and the last parameter (e) is used to capture the event from the keyboard.

this is the equilvalent function in my footer

function handleEvt(obj, pid, e){
    if (e.keyCode === 13) {  //where 13 is the enter button
      v = $(obj).val();
      alert('it is working');
    }
}

i try to run this code, but i'm not getting any alert, that is, the code is not working as expected, please can someone help me out..



via Andaeiii

Advertisement