Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #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.

  • #2
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,955
    Thanks
    9
    Thanked 725 Times in 719 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).

  • #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().

  • #4
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,955
    Thanks
    9
    Thanked 725 Times in 719 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:

    fishbaitfood (11-06-2011)

  • #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.

  • #6
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,955
    Thanks
    9
    Thanked 725 Times in 719 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.

  • #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!!

  • #8
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,955
    Thanks
    9
    Thanked 725 Times in 719 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.

  • #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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •