Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the new value of an HTML input after a keypress has modified it?

I have an HTML input box

<input type="text" id="foo" value="bar"> 

I've attached a handler for the 'keyup' event, but if I retrieve the current value of the input box during the event handler, I get the value as it was, and not as it will be!

I've tried picking up 'keypress' and 'change' events, same problem.

I'm sure this is simple to solve, but at present I think the only solution is for me to use a short timeout to trigger some code a few milliseconds in the future!

Is there anyway to obtain the current value during those events?

EDIT: looks like I had a caching problem with my js file as I checked the same code later on and it worked just fine. I would delete the question, but not sure if that loses rep for the kind folk who posted ideas :)

like image 743
Paul Dixon Avatar asked Oct 21 '08 17:10

Paul Dixon


People also ask

How do you find the value of an event input?

Get the value of input type checkbox The DOM query was done using document. getElementById('_id_'), which queries the document for elements with a matching id attribute. An element's Id is unique. Finding elements by Id ensures that you will only get a single element back.

What does onkeypress do?

The onkeypress attribute fires when the user presses a key (on the keyboard). Tip: The order of events related to the onkeypress event: onkeydown.


1 Answers

Can you post your code? I'm not finding any issue with this. Tested on Firefox 3.01/safari 3.1.2 with:

function showMe(e) { // i am spammy!   alert(e.value); } .... <input type="text" id="foo" value="bar" onkeyup="showMe(this)" /> 
like image 194
Owen Avatar answered Oct 02 '22 10:10

Owen