Get Id and move to another function on the click button

4

Galera,

I have the following problem, I have a table and every record I have a link.

By clicking this link I get the ID as follows and call a modal.

<script>
 $(document).on("click", "a", function(){
    if ($(this).hasClass("clique")) {
        var id = $(this).attr('id');
        $('#md-default').modal('show');
    }
 });
</script>

In this modal I have a confirmation button, how can I get the id that I clicked on the link? Is there a way to declare a global variable and store it in the ID value and then retrieve it by clicking the other button?

<script>
$('#submitButton').click(function() {
alert("Como pegar o ID alterior??");

}
</script>
    
asked by anonymous 22.11.2016 / 14:45

2 answers

3

If you choose to add to the global scope you could do something like:

<script>
 var idSel;
 $(document).on("click", "a", function(){
    if ($(this).hasClass("clique")) {
        idSel = $(this).attr('id');
        $('#md-default').modal('show');
    }
 });

 $('#submitButton').click(function() {
    console.log(idSel);
 });
</script>

 var idSel;
 $(document).on("click", "a", function(){
    idSel = $(this).attr('id');
 });

 $('#submitButton').click(function() {
    console.log(idSel);
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ahref="#" id="link">Clique em mim</a>
<br/><br/>
<button id="submitButton" >Ok</button>
    
22.11.2016 / 14:50
3

I suggest passing the ID to a date field of the submitting element.

This avoids global variables and the confirm button has the information it should have. It also fixes the adjustment of class clique , which thus exempts an unnecessary%%.

$(document).on("click", "a.clique", function(){
    $('#submitButton').data('id', this.id);
    $('#md-default').modal('show');
});
$('#submitButton').click(function() {
    var id = $(this).data('id');
    alert(id);
});
    
22.11.2016 / 16:39