Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-06-2011, 02:54 PM   PM User | #1
fishbaitfood
New Coder

 
Join Date: Nov 2011
Posts: 30
Thanks: 5
Thanked 0 Times in 0 Posts
fishbaitfood is an unknown quantity at this point
[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.
fishbaitfood is offline   Reply With Quote
Old 11-06-2011, 04:21 PM   PM User | #2
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,812
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
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).
SB65 is offline   Reply With Quote
Old 11-06-2011, 06:06 PM   PM User | #3
fishbaitfood
New Coder

 
Join Date: Nov 2011
Posts: 30
Thanks: 5
Thanked 0 Times in 0 Posts
fishbaitfood is an unknown quantity at this point
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().
fishbaitfood is offline   Reply With Quote
Old 11-06-2011, 07:26 PM   PM User | #4
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,812
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
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.
SB65 is offline   Reply With Quote
Users who have thanked SB65 for this post:
fishbaitfood (11-06-2011)
Old 11-06-2011, 08:01 PM   PM User | #5
fishbaitfood
New Coder

 
Join Date: Nov 2011
Posts: 30
Thanks: 5
Thanked 0 Times in 0 Posts
fishbaitfood is an unknown quantity at this point
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.
fishbaitfood is offline   Reply With Quote
Old 11-06-2011, 08:10 PM   PM User | #6
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,812
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
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.
SB65 is offline   Reply With Quote
Old 11-06-2011, 08:22 PM   PM User | #7
fishbaitfood
New Coder

 
Join Date: Nov 2011
Posts: 30
Thanks: 5
Thanked 0 Times in 0 Posts
fishbaitfood is an unknown quantity at this point
Hmm, if I use your code, it won't do anything, not even to the console.

EDIT: nevermind, it works now!

Thanks again SB65!!
fishbaitfood is offline   Reply With Quote
Old 11-06-2011, 08:23 PM   PM User | #8
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,812
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
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..
SB65 is offline   Reply With Quote
Old 11-06-2011, 08:28 PM   PM User | #9
fishbaitfood
New Coder

 
Join Date: Nov 2011
Posts: 30
Thanks: 5
Thanked 0 Times in 0 Posts
fishbaitfood is an unknown quantity at this point
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.
fishbaitfood is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:44 PM.


Advertisement
Log in to turn off these ads.