JQuery multiple event handlers

What does keyup () mean at the end of this word?

$("input").keyup(function () {
      var value = $(this).val();
      $("p").text(value);
    }).keyup();
+5
source share
2 answers

Thanks @nnnnnn sir.

The code binds the event keyupto everyone inputsalready owned by the DOM and immediately fires it for these inputs.

Now

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

the above code binds the event keyupto input, and the latter .keyup()makes the initial trigger for keyup.

You can also rewrite the code above:

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

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

Does it create an infinite loop?

NO . It only starts once when the page loads. See here

+5
source

The first .keyup is the binding method, the second is the trigger method:

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

All Articles