0

I have been using a script with jQuery 1.7 with the live() method here:

https://jsfiddle.net/anacaona83/umpdv484/

The remove() action with label "Supprimer" is all right.

When I try it with jQuery 2.x it does not work anymore so I changed live() to on() but then, the remove action does not work anymore:

https://jsfiddle.net/anacaona83/d87cj64L/

But the remove action does work when the

<TH> <a href="#" id="remScnt">Supprimer</a></TH>

Is not in the

javascript $(function() {

But in the HTML.

This is a problem because I really need to remove() the new <tr> I have created with the .appendTo() method.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
anacaona83
  • 23
  • 1
  • You can't just replace `.live` with `.on` - you need to read how it works. `.on` expects a container that is in the DOM at the time of binding to work. - https://jsfiddle.net/d87cj64L/2/ – tymeJV Dec 30 '15 at 17:03
  • 1
    Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Any code related to your question must be **in** your question, not just linked. Links rot, making the question and its answers useless to people in the future, and people shouldn't have to follow some random link to help you. Instead, put an [MCVE](/help/mcve) **in** the question (ideally using Stack Snippets, the `<>` toolbar button). – T.J. Crowder Dec 30 '15 at 17:05
  • 2
    Put your code in the actual question. Please don't put links – Registered User Dec 30 '15 at 17:05
  • Thanks for all your advices so I will do better next time :) This was my first questions. Have a good day :) – anacaona83 Dec 31 '15 at 08:02

1 Answers1

2

You need to apply on() to the parent element which is in the DOM on load of the page - your scntDiv element. Then you provide the selector of the element the event will be raised on in the second parameter, like this:

scntDiv.on('click', '#remScnt', function() {
    // your code...
});

Updated fiddle

Also note that you are appending elements with the same id attribute which will make the HTML of the page invalid, you should change those to classes.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • ++ for changing elements that have the same id into classes if we press the 'add' button multiple times, there are multiple elements with the `id` -> `remScnt` present on the page. every `id` needs to be unique. as a side note, to learn more about event delegation, go here: https://learn.jquery.com/events/event-delegation/ – taveras Dec 30 '15 at 17:13
  • Thanks a lot for your answer! This is very helpful as I am not at all a programmer... – anacaona83 Dec 31 '15 at 08:05
  • No problem, glad to help. – Rory McCrossan Dec 31 '15 at 09:21