0

so sorry for the wired title, i could not explain the problem in few words in the title. I am very new in javascript i have a problem.

I have a title for users that should be replaced by few text input (first name, last name ... ) when user add the data, the title change automatically. Then, an edit button appear under the title. In my code, when user click on the edit button, the text inputs appear again to change the data. The problem is, the edit button, only works in the first time. And then we save the data again, the edit button does not works anymore.

as i know, when new data have been entered, the server change the title with html. The new title that contains the entered data have edit button as well, the new edit button that comes with the new title, does not work with the codes.

here is the code for the edit button:

events(){

$(".js-edit-name").on("click", this.openEditor.bind(this));

}

openEditor(){
this.body.find(".form-input, .form-submit").show("slow");
this.body.find(".form-block").removeClass("hidden");
this.body.find(".js-edit-name").addClass("hidden");
this.body.find(".js-cancel-name").removeClass("hidden");
}

here is the code for the edit button:

<div class="icon positive positive-inverse small js-edit-name">
<svg class="svg-inline--fa fa-pencil-alt fa-w-16" aria-hidden="true"  data-prefix="fas" data-icon="pencil-alt" role="img"   xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg>…. </svg>
<!-- <i class="fas fa-pencil-alt"></i> -->
" edit"

I hope i explain the problem clearly. Could anyone help me. Thanks a lot

user9217262
  • 35
  • 1
  • 1
  • 8

1 Answers1

0

You say that your edit button gets replaced, that means when the old button is removed its attached event will no longer work. So instead of:

$(".js-edit-name").on("click", this.openEditor.bind(this));

You should do this:

$(document.body).on('click', '.js-edit-name', this.openEditor.bind(this));

This will allow your handler to run for all .js-edit-name elements no matter when they get attached to the dom.

Amr Noman
  • 2,597
  • 13
  • 24