5

What does the keyup() at the end of it mean?

$("input").keyup(function () {
      var value = $(this).val();
      $("p").text(value);
    }).keyup();
2
  • Doesn't this create an infinite loop ? Commented Jun 27, 2012 at 12:16
  • @nnnnnn You are right. Prorgrammers always learn I guess lol Commented Jun 27, 2012 at 12:40

2 Answers 2

5

Thanks @nnnnnn Sir.

The code bind a keyup event to all inputs already belong to DOM and immediately trigger it for those inputs.

Now,

$("input").keyup(function () {
  var value = $(this).val();
  $("p").text(value);
});

above code bind the keyup event to input and last .keyup() makes an initial trigger to keyup.

You can rewrite above code also as following:

$("input").keyup(function () {
      var value = $(this).val();
      $("p").text(value);
    })

$('input').keyup(); // or $('input').trigger('keyup');

Does it create an Infinite loop?

NO, It triggers just one time at page load. See here

Sign up to request clarification or add additional context in comments.

4 Comments

Does it really do that after page load? Is it standard? TIL if ever. :)
@RichardNeilIlagan yup it does, if the code wrapper within jQuery DOM ready function
Note that it doesn't necessarily have to be in a DOM ready function: jsfiddle.net/QStkd/507 It would be more accurate to say that the code binds a keyup handler to any input elements already in the DOM and immediately triggers a keyup event for those elements.
@thecodeparadox ooppss... Sorry, I forgot to. It told me to accept it in 10 minutes, but it looks like I procrastinated accepting it lol
1

The first .keyup is a binding method the second one is a trigger method:

$("input")
    .keyup(function () { // Bind on keyup
        var value = $(this).val();
        $("p").text(value);
    })
    .keyup(); // Trigger keyup

Comments

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.