1

I'm working on a dynamic calculation program to practice jquery, but so far it's not going well, I can store the values in a variable, of course (see code here).

<form>
    Tafeltje van:
    <input type="number" name="tafel" id="tafel" />

    Aantal:
    <input type="number" name="aantal" id="aantal" />
</form>
<div id="output"></div>

and the JS:

var tafel = $('#tafel').val();
var aantal = $('#aantal').val();

How would one be able to print out these values in output while the user is typing in the text fields?

1
  • use .keyup() event and in keyup event add the values then out put it using .text() or .html() Commented Jun 24, 2016 at 7:40

1 Answer 1

1

You can bind your code with keyup or input event of the inputs. Then, once you have got the values, you can use either text() or html() to display the values in #output div in whatever format you want.

// $("input").on("keyup", function(){
$("input").on("input", function(){
  var tafel = $('#tafel').val();
  var aantal = $('#aantal').val();

  $("#output").text("tafel: " + tafel + " aantal: "+aantal);
});//keyup
Sign up to request clarification or add additional context in comments.

4 Comments

keydown would make more sense, wouldn't it ? In case user keeps holding a key.
@you.know.nothing Yes, I checked around after your comment and I think native input event is more suited for it then. Let me know if keydown is better than input. Thanks for pointing out :)
That's even better :)
holy, totally forgot to set this as the answer, only just saw it as it popped up as getting 1k views, anyway, set as answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.