2
votes

I have a form with input fields (storing prices for each object) that have different events for keyup, blur, and keypress. When the user tabs through the fields, my JS loops through each input field to check if it has been changed from its previous value (and perform a calculation based on that). (I tried using the .change and .keypress events instead - unfortunately they were timed to fire AFTER keyup, and it needs to be before).

I store a global variable array of the each input's initial value and check the current value against that. But for some reason, when I loop through each input and check its .val(), the effect is that the user's currently selected input gets deselected. Same happens for .attr("value"). I've debugged sufficiently to know that this particular call is the issue - substituting a random number instead of calling .val(), for example, causes it to work perfectly. Any ideas why this is happening?! I'm at my wit's end.

1
You should consider dropping your code in jsfiddle.net and sharing it here.Bob.
Have you tried with onkeydown event? That would fire before keypress and keyup. From w3schools.com: The order of events related to the onkeydown event: onkeydown -> onkeypress -> onkeyupTildalWave
You're a lifesaver. Thanks so much!user1436111

1 Answers

1
votes

For the sake of clarity, and not wanting to leave this question appear unanswered, I'm repeating the accepted answer from the comments section also here:

The order of events related to the onkeydown event: onkeydown -> onkeypress -> onkeyup

Answer taken from w3schools.com. Using onkeydown event instead seems to have solved the OP's dilemma:

(I tried using the .change and .keypress events instead - unfortunately they were timed to fire AFTER keyup, and it needs to be before)

;)