Enjoy an ad free experience by logging in. Not a member yet?
Register .
11-06-2011, 02:54 PM
PM User |
#1
New Coder
Join Date: Nov 2011
Posts: 30
Thanks: 5
Thanked 0 Times in 0 Posts
[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.
11-06-2011, 04:21 PM
PM User |
#2
Senior Coder
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
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).
11-06-2011, 06:06 PM
PM User |
#3
New Coder
Join Date: Nov 2011
Posts: 30
Thanks: 5
Thanked 0 Times in 0 Posts
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().
11-06-2011, 07:26 PM
PM User |
#4
Senior Coder
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
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.
Users who have thanked SB65 for this post:
11-06-2011, 08:01 PM
PM User |
#5
New Coder
Join Date: Nov 2011
Posts: 30
Thanks: 5
Thanked 0 Times in 0 Posts
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.
11-06-2011, 08:10 PM
PM User |
#6
Senior Coder
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
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.
11-06-2011, 08:22 PM
PM User |
#7
New Coder
Join Date: Nov 2011
Posts: 30
Thanks: 5
Thanked 0 Times in 0 Posts
Hmm, if I use your code, it won't do anything, not even to the console.
EDIT: nevermind, it works now!
Thanks again SB65!!
11-06-2011, 08:23 PM
PM User |
#8
Senior Coder
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
What browser are you using - I only tested in FF7.
EDIT:Works in IE8 as well.
Last edited by SB65; 11-06-2011 at 08:26 PM ..
11-06-2011, 08:28 PM
PM User |
#9
New Coder
Join Date: Nov 2011
Posts: 30
Thanks: 5
Thanked 0 Times in 0 Posts
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.
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 02:23 PM .
Advertisement
Log in to turn off these ads.