Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery keypress() event get text

I want a function to be run when a keypress occurs on a text box, so I have this code:

$("input[x]").keypress(function() {         DoX();     }) 

This is working fine, but in my function I want to do something based on the text value in the textbox

var textValue = ("input[x]").val(); 

Now the problem here is that it lags behind by a key so if my text box says 'He' and I type an 'l', then I want my textValue to be 'Hel', but it is returning the previous value 'He' because presumably the character hasn't been put in the text box yet.

Is there a way of getting 'Hel' out of my function here?

Thanks :)

like image 311
Fiona - myaccessible.website Avatar asked Sep 11 '09 15:09

Fiona - myaccessible.website


People also ask

How do you value a keypress event?

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

How does jQuery detect keyboard press?

jQuery | keypress() The keypress() method in jQuery triggers the keypress event whenever browser registers a keyboard input. So, Using keypress() method it can be detected if any key is pressed or not.

What is Keyup and Keydown in jQuery?

jQuery keyup() Method The order of events related to the keyup event: keydown - The key is on its way down. keypress - The key is pressed down. keyup - The key is released.

Is keypress deprecated?

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes.


1 Answers

You can try to use the keyup event:

$(selector).keyup(function() {   var textValue = $(this).val();   DoX(); }); 
like image 81
Christian C. Salvadó Avatar answered Sep 18 '22 22:09

Christian C. Salvadó