Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get input text value on jQuery keypress event [duplicate]

Possible Duplicate:
jQuery get input value after keypress

I'm trying to get an input text value on jQuery .keypress() function. I've seen various examples with keypress and keydown, but not dedicated on getting the input value. Is it possible?

$(document).ready(function(){
    $("#my_field").keydown (function (e) {
        alert (e);
    });
});

The returned object has a series of properties but I haven't seen something for value input field attribute.

Is there some way to get it?

like image 985
vitto Avatar asked Aug 07 '10 13:08

vitto


2 Answers

I'm trying to get a input text value

Use the val():

$("#my_field").keydown (function (e) {
    alert ($(this).val());
});

Assuming that #my_field is the id of input field you want to get the value of.

like image 67
Sarfraz Avatar answered Sep 21 '22 11:09

Sarfraz


I'm unclear which you're after here, in case you're after the letter pressed, not the whole value, you can use String.fromCharCode(), for example:

$(document).ready(function(){
  $("#my_field").keydown (function (e) {
     alert (String.fromCharCode(e.which));
  });
});

You can give it a try here

like image 20
Nick Craver Avatar answered Sep 22 '22 11:09

Nick Craver