1
Good Morning,
I’m working on a page where there are several ul
dynamic, that is to say with li
being implemented in them via input and counted through several variables (one for each ul
), and to make it easier I made a Delete function li
global for all my ul
, the function was Thus:
$('.excluir_li').on('click', function(){
var elemento = $(this);
do{
if(elemento.is('li')){ //Busca o Li a partir do botão de Excluir
li = true;
}else{
elemento = elemento.parent();
}
}while(!li);
switch(elemento.attr('class')){ //Indentifica de qual UL ela faz parte e atualiza o seu contador
case 'sitProblemaSalvo': contSitProblema--; break;
case 'SitAprendizagemSalvo': contSitAprendizagem--; break;
case 'compTecnicoSalvo': contCompetenciasTecnicas; break;
case 'compGestaoSalvo': contCompetenciasGestao--; break;
case 'critCompTecnicoSalvo': contCriteriosCompTecnico--; break;
case 'critCompGestaoSalvo': contCriteriosCompGestao--; break;
case 'planoAulaSalvo': contPlanoAula--; break;
case 'estrategiaPlanoAulaSalvo': contEstrategia--; break;
case 'recursoPlanoAulaSalvo': contRecurso--; break;
case 'intervencaoPlanoAulaSalvo': contIntervencao--; break;
}
elemento.remove();// remove a LI
}
});
However when executing I noticed that the function runs several times(the number of li
list to be more exact), I researched and tried to use the function e.preventDefault();
but this was not effective, could anyone help me?
Are you running this snippet of code more than once? If yes, you will end up assigning the event more than once to each existing read at the time of execution.
– bfavaretto
i am running this event within a function and I am calling it every time I will add a LI, I did this way why if the event did not link...
– Miguel Carigo
The ideal in these cases is to create a single Event Listener in a higher element in the hierarchy, and that is not dynamic. This is called event delegation. But you can’t say more than that without knowing your html.
– bfavaretto