-2

I've seen various ways to attach event handlers using JQueryy and although each seem to work, I'm just not sure if they are at all different or if either are deprecated.

$(document).on("click", ".someSelector", function (event) {
  // do work...
});

$(function () {
  $(".someSelector").click(function (event) {
    // do work...
  });
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Steven Frank
  • 551
  • 3
  • 16

1 Answers1

2

The first example works whether or not there's an element on the page with class "someSelector". If one is added eventually, it'll work.

The second only applies the event handler to existing elements. If an element is added later, "click" events won't be handled.

Pointy
  • 405,095
  • 59
  • 585
  • 614