Event click multiplying

Asked

Viewed 51 times

1

I have a directory listing system that displays files and folders, when I click on an item, it displays a (single) menu that is already set on the page and can be used by all files. In this menu, there is an option called DELETE, the problem is there. To open the menu have to click, however, I need to know what was the id of the file chosen to use in the deleteFile function:

var files = document.getElementsByClassName("backgroundFiles");
for(i=0;i<files.length;i++){
    files[i].addEventListener("click", function(event){
        menuFiles(this.id, event.clientX, event.clientY);
        deleteFile(this.id);
    });
}

In deleteFile there is another click event that will be triggered when the user clicks on the DELETE menu option:

function deleteFile(idElement){
    document.getElementById("a_dele").addEventListener("click", function(event){
        var chave = prompt("Digite a chave de segurança");
        ...
    });
}

The problem is that these events are multiplying, when giving the first click everything is ok, already in the second, appears twice the prompt, in the third click appears three times the prompt and so on. Someone could help me with a solution?

  • Try to put Event.stoppropagation() in click function, this serves to click not propagate on the site.

  • This occurs when you run addEventListener several times. In case you are running this for every time you click on menu, there is the error.

  • So I’ve tried this with both stopPropagation() and stopImmediatePropagation() but without success. As for, it only runs once to each directory request, in case I double click on a folder to open, this will be redone only for new files.

  • The function delete should be called from within menuFiles. You can show the code of menuFiles?

  • I was able to solve the problem by replacing addeventlistener() by onclick() inside deleteFile, I don’t know if it would be the right way but it’s working normally. Thank you all for your attention.

No answers

Browser other questions tagged

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