0

I would like to handle a change event on elements dynamically created, and selected with each():

$("input[type=date]").each(function() {
    $("#myForm").on("change", this, function() {
        $(this).css({ color: "#B73132" });
    });
});

on() doesn't seem to work.

I tried:

$this= $(this); // and    $this= this;with
$("#form_cahier").on("change", $this, function() {

Nothing works, any idea ?

xavier bs
  • 1,341
  • 1
  • 12
  • 9
  • The best method to use when dealing with dynamically appended elements is to use delegated event handlers. See the duplicate question's answer for the best methods to follow. – Rory McCrossan Jan 19 '15 at 16:42
  • You would never need an `each` if done properly. Use `$("#myForm").on("change", "input[type=date]", function(){`. It runs the jQuery selector *at event time* on the events bubbled up to the ancestor (e.g. `#myForm`). – iCollect.it Ltd Jan 19 '15 at 16:51
  • @Rory McCrossan: He *was* trying to use it as a delegated handler, but got the syntax wrong (passing `this` instead of a selector). – iCollect.it Ltd Jan 19 '15 at 16:54

0 Answers0