javascript/jquery input fields cleanup

Posted by user271619 on Stack Overflow See other posts from Stack Overflow or by user271619
Published on 2010-04-01T22:15:26Z Indexed on 2010/04/01 22:23 UTC
Read the original article Hit count: 388

Filed under:
|
|

I've created a few input fields, that I am cleaning up as the user types.

So, I'm using a keystroke detection event, like .keyup()

It's all working very well, but I do notice one thing that's rather annoying for the users.

While the script is cleaning the data as they type, their cursor is being sent to the end of the input field.

So, if you want to edit the middle of the value, you're cursor immediately goes to the end of the box.

Does anyone know of a way to maintain the cursor's current position inside the input field?

I'm not holding my breath, but I thought I'd ask.

Here's the cleanup code I'm using:

$(".pricing").keyup(function(){

   // clean up anything non-numeric
   **var itemprice = $("#itemprice").val().replace(/[^0-9\.]+/g, '');** 

   // return the cleaner value back to the input field
   **$("#itemprice").val(itemprice);**

});

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about replace