0
When you click on a button to delete an item from a list using Javascript, it only executes the action after the 2nd click. I would like to know what I must do to make the function work correctly from the 1st click.
Modal bootstrap
<!-- Modal - remover estado do serviço -->
<div class="modal bd-example-modal-sm" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="exampleModalLabel">Deseja remover este estado do serviço?</h4>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" id="btn-remover-estado" onclick="return removerEstado();">Sim</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Não</button>
</div>
</div>
</div>
</div>
Javascript function
function removerEstado(){
$('#btn-remover-estado').click(function(){
let estados = document.getElementById('lista-estados');
estados.remove(estados.selectedIndex);
});
}
Victor, apparently you still don’t understand how events work in javascript. When you say
onclick=uma_funcao()
you are saying to perform that function when the element is clicked. When you do$('#meu-id').click(uma_funcao)
you are doing the same only in a different way. I explain better about events and jQuery in this answer, if interest.– fernandosavio
Yes, Fernando. My javascript skills are still very low. I will take advantage of the answer you gave me to understand more about this topic. Thank you.
– Victor Moraes