Jquery I have 2 document on click, and it only works if I remove 1

0

Hello, I have two functions, one that eliminates rows, and another that edits, this I carry out with $(document).on('click', function...);

Function 1:

$(document).unbind('click').on("click",".btr",function(){
    alert('click');
    if( $("#articulos tr").length > 2 )
    {
        var parent = $(this).parents().parents().get(0);
        $(parent).remove();
    }//end if });

Function 2:

$(document).unbind('click').on('click', '.bte', function(){
    //extraigo el index
    index = $('.bte').index(this);
    //extraigo el nombre con index
    nombre = $('.articulo').eq(index).val();
});

THE PROBLEM :. If I remove one, the other works correctly, but if I have both, it does not work for me.

    
asked by user26815 03.01.2017 в 03:12
source

1 answer

3

The main problem is that you are using

$(document).unbind('click').on("click",  ...

for both events, so you are calling .unbind() in both cases associated with $(document) . That is, calling the second unbind() is eliminating the event that was just associated.

Instead, you could call .unbind only for the specific class to which we associated the event. One for class btr :

$(".btr").unbind('click').on("click",function(){
    // Código del evento 
});

And another for class bte :

$(".bte").unbind('click').on("click",function(){
    // Código del evento 
});
    
answered by 03.01.2017 в 03:31