CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript frameworks (http://www.codingforums.com/forumdisplay.php?f=62)
-   -   [jQuery] Entered digits in input field to be processed as one number (http://www.codingforums.com/showthread.php?t=243062)

fishbaitfood 11-06-2011 02:54 PM

[jQuery] Entered digits in input field to be processed as one number
 
Hello everyone,

I'm figuring out how to process entered digits as one number.

For now, I'm only able to process the input with the last entered, single digit.

Is there a way to store the entered digits to a string, and then make that string a number again?
Or maybe something more sufficient?

Code:

$("input.input-amount").live("keypress", function (e) {
        if (e.keyCode >= 48 && e.keyCode <= 57) {
                var amount = String.fromCharCode(e.keyCode);
                $(this).val(amount);
                var price = $(this).parent().next().find("input.input-price").val();
                var total = amount * price;
                $(this).parent().next().next().find("input").val(total);
        }
});

Thank you.

SB65 11-06-2011 04:21 PM

Run the event on the change event rather than the keypress:

Code:

$("input").live("change", function () {
    alert (parseInt($(this).val()));
});

and use parseInt to force to an integer (you might need some validation as well).

fishbaitfood 11-06-2011 06:06 PM

Hey SB65,

This works, thank you, but my intention was to get the value as soon as you enter a number, for instant calculations.
That's why I thought maybe, with the use of my first example, to convert the entered digits to a string, and then back to a number?
Now it kinda behaves like .blur().

SB65 11-06-2011 07:26 PM

OK.

Maybe I'm missing something but why do you need to go via a string? Won't:

Code:

                $("input").live("keyup", function (e) {
                if (e.keyCode >= 48 && e.keyCode <= 57) {
                                console.log(parseInt($(this).val()));
                        };
                });

do what you need? So if the user types 123 into the input the code will produce 1, 12, 123 as the keys are pressed - is that what you want?

From a brief test in FF7 (only) I got better results with keyup compared to keypress, incidentally.

fishbaitfood 11-06-2011 08:01 PM

Yes, 1, 12, 123, is what I had already.
But when getting that value, I only get the last entered digit, instead of the whole value.

So, as I'm entering digits, it needs to continuously adjust the value, so it'll get the whole number, instead of only the last digit.

I hope this makes some sense to you.

SB65 11-06-2011 08:10 PM

Well, the code in my last post will produce 1, 12, 123, not just the last digit.

Your original code produces the value of the keypress - so you get 1, 2, 3, but what you want, I think, is the whole value of the input field - which is what this gives you.

fishbaitfood 11-06-2011 08:22 PM

Hmm, if I use your code, it won't do anything, not even to the console.

EDIT: nevermind, it works now! :D

Thanks again SB65!!

SB65 11-06-2011 08:23 PM

What browser are you using - I only tested in FF7.

EDIT:Works in IE8 as well.

fishbaitfood 11-06-2011 08:28 PM

It's for personal use only, and it works in the browsers I need, so I'm set! ;)

Thanks a bunch, SB65, I think I'm gonna use .live a hell of a lot more from now on. :)


All times are GMT +1. The time now is 05:08 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.