(Javascript) - How can I remove duplicate event occurrence

Asked

Viewed 73 times

1

Good evening guys, I am learning Javascript and decided to create this layout to test some knowledge, created a table and a modal to add users to the table.

I’m creating the part of edit users, and for this, each user inserted in the table has a button edit, so I thought I’d add a click event so that when the user clicks on the edit button, he can run the rules I need.

The problem I am having is the following, it adds the click event normally, only that to each new user inserted in the table, it is duplicating the click event, and this cannot occur.

In the example below, I have 3 users entered, and when I clicked the edit button on the first user, it triggered the click event 3 times.

erro

The code I did to test is this, where it has a method called edit:

editUser() {
    let editButton = [...document.querySelectorAll('[data-edit]')];
    editButton.forEach((element) => {
        element.addEventListener('click', () => {
            console.log('Cliquei!');
        });
    });
}

This dataset called date-Edit it is present on all edit buttons, use it to locate these buttons and apply the click event.

I would love to solve this problem I tried to get around by this other means but it did not work:

editUser() {
    let fn = (event) => {
        console.log('Cliquei', event);
    }

    let arr = [];
    for (let i of [...document.querySelectorAll('[data-edit]')]) {
        arr.push(i);
    }

    for (let i of arr) {
        i.addEventListener('click', fn);
    }

    for (let i of arr) {
        i.removeEventListener('click', fn);
    }

    for (let i of arr) {
        i.addEventListener('click', fn);
    }

}

I believe the problem is that this method, is being called whenever it adds a new user to the table, but I could not get around this problem, could anyone help me? thank you in advance.

If anyone wants the code here: hmtl: https://pastebin.com/vRK6XhZj index: https://pastebin.com/m42pj29S Class code: https://pastebin.com/33WXMaZ6

  • 1

    the problem is this selector document.querySelectorAll('[data-edit]') that will always pick up all the elements again, need to associate the event only to the new element that has been added, or do the removeEventListener before to remove all events before associating new ones

  • @Ricardopunctual thank you, I even suspected it was this, because each new user was repeating the same event, anyway, I thought of another approach and it seems to have worked, I did the following, I did the location of the elements by id, and saved their location in an array, after that I used the foreach() method to go through each element, and I used the IF conditional to check if that element has a dataset called edit if you own do nothing, if you don’t have create that dataset and add the click event to that element.

  • 1

    sounds like a good idea. I’ll even suggest another: on addUser, when generating html you can put an id on a sequential number in buttom with the data-edit, after making the append of tr, you can associate the event by clicking on this button that already has id, without having to call editUser. Or even simpler, make the selector from the tr, because it will only find the new button, like this: tr.querySelector('[data-edit]'), seems simple :)

  • @Ricardopunctual show, thank you so much for the alternatives and for giving this strength ai vlw.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.