How to recognize button/element click inside a modal window?

Asked

Viewed 634 times

1

How to recognize button/element click inside a modal window?

I tried the following, without success:

$("#modalEscolha").bind('click', function () {
    if ($("#escolherComp").data('clicked')) {
        alert("Componente!");
    } else if ($('#escolherKIT').data('clicked')) {
        alert("KIT!");
    } else {
        $('#acionamento').prop('selectedIndex', 0);
    }
});

Editing:

I own a input select, which must be set according to the closing of the modal or the clicking of the buttons: escolherComp or escolherKIT. If the user closes the modal, or by clicking outside the body, or the close button, the index 0 should be set, otherwise if any of the buttons click occurs the set option should remain (no action is executed).

  • Have you tried using . on instead of . bind?

  • Hello @Dvd, not solved.

  • From what I understand, your question would be "How to set index 0 in the element $('#acionamento') when the modal is closed?".. Would this be?

  • @DVD, yes and when none of the buttons are clicked.

  • What are these buttons?

  • #escolherComp and #escolherKIT @Dvd

  • Try changing only the first line to $("#modalEscolha, #modalEscolha input").on('hidden.bs.modal click', function () {.

  • Why don’t you post the HTML?

  • HTML is "huge". https://pastebin.com/hETWM3vq

Show 5 more comments

1 answer

2


As explained in the chat, you have the following conditions:

  • 1º when selecting the motorized option is opened the modal with the two options
  • 2º if the user does not click on any option at that time and simply close the modal the input select is reset.
  • 3º in case the user click on any of the options that is in the modal, the motorized option is set and the modal is closed
  • 4º after clicking on any of the options in the modal is created a button "called" Change, which opens the modal again
  • 5º from that moment, as the option "Motorized" will already be selected, if it simply closes the modal again should NOT be reset

Upshot:

$(document).ready(function(){
var setarZero="0";
var noBodyFunction="0";
  $('select').on('change', function () {
    if ($(this).val() == "Motorizado") {
        $('#modalEscolhaAc').modal('show');
    }else{
        $("#trocarAc").hide();
        setarZero = ($("#acionamento").prop('selectedIndex'));
    }
  });

  $("#escolherComp, #escolherKIT").on('click', function () {
    $('#acionamento').prop('selectedIndex', 7);
    $("#trocarAc").show();
    setarZero=7;
      if ($(this).attr('id') == 'escolherComp') {
         alert("Componente!");
      } else if ($(this).attr('id') == 'escolherKIT') {
         alert("KIT!");
      }
  });
  
  $("#trocarAc").on('click', function () {
	   $('#acionamento').prop('selectedIndex', 7);
	   noBodyFunction="1";
  });

  if(noBodyFunction!="1"){
    $("body").click(function() {
	  target = document.getElementById("modal-footer");
	  flag = event.path.some(function(el) {
	     return (el == target)
	  });
	    
	  if ((!flag)&&(setarZero !="7")) {
	     $('#acionamento').prop('selectedIndex', setarZero);
	  }
    });
  }	     

});	
    #escolherKIT{
       background-color: #d3e5f2;
       margin: 3px; padding: 3px;
       height:30px;
       width:200px
    }
    #acionamento{
	   width:190px;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 

<!-- Modal -->
<div id="modalEscolhaAc" class="modal fade" role="dialog">
    <div class="modal-dialog">
	<!-- Modal content-->
	<div class="modal-content">
	    <div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal">&times;</button>
	        <h4 class="modal-title" style="color:#000">Acionamento motorizado</h4>
	    </div>
	    <div id="modal-footer" class="modal-footer">
	        <input type="button" name="escolherComp" id="escolherComp" value="Escolher componentes para automação" data-dismiss="modal" class="btn btn-success">
	        <input type="button" name="escolherKIT" id="escolherKIT" value="Escolher KIT" data-dismiss="modal" class="btn btn-primary">
	    </div>
	</div>
    </div>
</div>
<!-- Fim modal -->
	
	
<select name="acionamento" id="acionamento" required="true" class="form-control">
	<option value="">Selecione um produto</option>
	<option value="Bilateral">Bilateral</option>
	<option value="Central">Central</option>
	<option value="Convencional">Convencional</option>
	<option value="Haste e corda">Haste e corda</option>
	<option value="Invertido">Invertido</option>
	<option value="Lateral">Lateral</option>
	<option value="Motorizado">Motorizado</option>
</select> <input type="button" value="trocar" id="trocarAc" onclick="$('#modalEscolhaAc').modal('show')" style="display: none;"/>

  • Leo, here’s the thing: your solution works as long as you consider just clicking on the buttons. If the user does not click on any of the buttons, that is, simply close the modal (by clicking off the body or close) the condition else {$('#acionamento').prop('selectedIndex',0);} has to be executed. Otherwise, the others before. @Leocaracciolo

  • Leocaracciolo, we can continue this discussion chat?

  • @luccasrodrigo edited the answer

  • Hello Leo, you can follow the discussion via chat? I’m sorry, but I believe there are some details missing from the question.

Browser other questions tagged

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