0

The .click in jquery is deprecated. So I made amendments to the existing code, however, there seems to be some issue.

    GetPageButtons() {
    let $saveButton = $("<input />", {
        type: "button",
        value: "Save",
        class: "button large save",
      }),
      $cancelButton = $("<input />", {
        type: "button",
        value: "Cancel",
        class: "button large cancel",
      });
    $saveButton.click(() => {
      this.UpdateRecord();
    });
    $cancelButton.click(() => {
      this.modal.success = true;
      let _parent = this;
      $.each(_parent.fields, function (i, field) {
        delete this.newValue;
        this.element.val("");
      });
    });
    return [$cancelButton, $saveButton];
  }

The above code works, although my linter complains saying it deprecated.

  GetPageButtons() {
    let $saveButton = $("<input />", {
        type: "button",
        value: "Save",
        class: "button large save",
      }),
      $cancelButton = $("<input />", {
        type: "button",
        value: "Cancel",
        class: "button large cancel",
      });
    $saveButton.on("click", function() {
      this.UpdateRecord();
    });
    $cancelButton.on("click", function() {
      this.modal.success = true;
      let _parent = this;
      $.each(_parent.fields, function (i, field) {
        delete this.newValue;
        $( this ).element.val("");
      });
    });
    return [$cancelButton, $saveButton];
  }

The refactor to the suggested use of .on('click', fn){} however is not functioning. I get an error that says:

"Uncaught TypeError: this.UpdateRecord is not a function".

Why? It should work the same as the .click which is shorthand for .on. Any suggestions???

1 Answers1

0

This does not work, because you are using normal function in contrast to the original code that used arrow function.

This means that the this inside the functions refers to something different.

Change it to

$saveButton.on("click", () => {
  this.UpdateRecord();
});
$cancelButton.on("click", () => {
  this.modal.success = true;
  let _parent = this;
  $.each(_parent.fields, function(i, field) {
    delete this.newValue;
    $(this).element.val("");
  });
});

and it should work as originally.

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317