Function jQuery called by two distinct elements at once

Asked

Viewed 501 times

0

I have the following code:

<table>
[...]
<tr class=""> 
     <td class="col-md-2">
        10.1
     </td> 
     <td class="col-md-8"> 
        Teste 
     </td>  
     <td class="col-md-1"> 
        10 
     </td>  
     <td class="col-md-1">  
        <button type="button" class="btn btn-danger remover" data-toggle="modal" data-target="#confirma">  
            <span class="glyphicon glyphicon-trash"></span>  
        </button>  
     </td>  
</tr>
[...]
</table>

JS:

function removeEvento() {
    jQuery(".remover").click(function(e) {
        e.preventDefault();
        var linha = jQuery(this).parent().parent();
        jQuery("#confirma").modal("show");
        jQuery("#sim").click(function(e) {
            linha.remove();
            jQuery("#confirma").modal("hide");
        });
        return false;
    });
}
function insereEvento(formName, codInput, valoresArray, tabela, item, pontos) {
    tabela.append(linha);
    removeEvento();
    return true;
}
  • It runs every time the delete line button is pressed.
  • There is a button of this type for each row of a table.
  • These lines are inserted by jQuery/Javascript, so I can’t reload the page.
  • Problem: When I click the delete button, it opens a modal window, to delete the line, whether I click outside or not, the modal closes. The problem is that when I click to delete another record and delete it, the previous record is also deleted.

Thanks in advance.

  • 1

    Shows html too, please. You have some unique identifier of the line you want to delete?

  • No, to delete I’m doing the following: I pick the handle by clicking, use Parent twice, catching the <tr>. And then I give a line.remove().

  • And where you call function removeEvento()?

  • In the function inserts Wind(). In it I add an event and call the function, otherwise it would not work, I think because the element was added after loading the page, so calling the function works.

  • Please edit your question when you need to add more code to it.

  • Gabriel, if any of the answers answered you, mark it as accepting that the question is complete. Unlike a forum, stackoverlow is a Q&A, where a question is considered "solved" when one of the answers is marked as accepted.

Show 1 more comment

2 answers

2


The problem is that every time you click the "button. remove" you are creating an Handler to the "#yes" button with the current value of "line" (via closure). That way when the user clicks the "yes" button it will run all the handlers. Therefore, to fix this you can declare the variable "line" outside the scope of Handler and create the "#yes" button Handler only once:

function removeEvento() {
    var linha;
    var confirmaModal = jQuery("#confirma");

    jQuery(".remover").click(function(e) {
        e.preventDefault();
        linha = jQuery(this).parent().parent();
        confirmaModal.modal("show");
        return false;
    });

    jQuery("#sim").on("click", function(e) {
        linha.remove();
        confirmaModal.modal("hide");
    });
}

1

As you are using a table, do the following: change the row

var linha = jQuery(this).parent().parent();

and put that in place:

var linha = jQuery(this).closest('tr');

This operation is "cheaper" and faster.

And then your method removeEvento may be setting several actions on the same modal button since, for each time you open the modal, you are adding a new modal event click.

In the architecture you defined, you should clear the modal events before adding a new one. To clean use .off().

Your code should look something like this:

function removeEvento() {
    jQuery(".remover").click(function(e) {
        e.preventDefault();
        var linha = jQuery(this).closes('tr');
        jQuery("#confirma").modal("show");
        jQuery("#sim").off("click", "**").click(function(e) {
            linha.remove();
            jQuery("#confirma").modal("hide");
        });
        return false;
    });
}

Off documentation: http://api.jquery.com/off/

Documentation of the Closest: http://api.jquery.com/closest/

  • good response, just to complement, the function .unbind() may be used instead of .off(), as it receives as parameter only the event

  • Thanks for the reply, I will test later and return warning.

  • Use the $.parents instead of the $.closest would be more appropriate => http://api.jquery.com/parents/

  • @Felipeasuncion Not really. $.parents will return all parents of the element that match the specified selector. Even if in this scenario there is only one parent TR, this element is returned within a array. Thus, to execute the remove the jQuery need to traverse an element’s array. $.closest returns only one element. O jQuery executes the action on this element only. Thus, use $.closest has slightly better performance.

  • @Frenetic, I did it the way you said, it didn’t work, it kept giving the same problem. I did it the way Cesar William commented and it worked.

  • @Gabrielorabonicarvalho Good that solved! : D

  • @Frenetic, thank you so much for the help man.

Show 2 more comments

Browser other questions tagged

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