views:

48

answers:

4

Hi, I'm trying to get a input text value on jQuery .keypress() function, I've saw 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 saw something for value input field attribute.

Does exists some way to get it?

A: 

You could look at event.which:

$(function() {
    $('#my_field').keydown(function(e) {
        alert(e.which);
    });
});
Darin Dimitrov
A: 

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

Nick Craver
+1  A: 

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.

Sarfraz
it's perfect, thank you very much!
Vittorio Vittori
@Vittorio Vittori: You are welcome :)
Sarfraz
A: 

If you want the whole input field value look at this where your alert is:

$(this).val()
Tahbaza