How to run double event with jquery

-2

As I execute two events $ (". example"). click (function () {in this query, it is possible to assign two selectors to execute the same event.

To be more exact I can execute click event to remove detail and add. that are in one and not separately

Normally jQuery associates the events when the page loads (the document.ready).

If you create new elements after running the document.ready they will not have the associated events. You will have to associate them again.

For that reason I put a oneclick inside another because the before adds a new html to the sun. but if I put them separately the variable that comes from before the event below does not hold it because effectively when the page starts the data does not exist. how do I solve the problem thanks

Can you give me an example?

  $(".agregar").click(function(){



codigoproducto =$(this).parent().parent().children(".codigoproducto").html();

$(".agregarproducto").before(' <tr class="trjson"><th class="codigoproducto">'+codigoproducto+'</th></tr>');


$(".eliminardetalle").one('click', function() {

$(this).parent().parent().remove();
codigoproducto=$(this).parent().siblings(".codigoproducto").html();
console.log(codigoproducto);

});


});

//////////////////////////////////////////////////////////////////////////

      $(".agregar").click(function(){


///agregar carga variable
    codigoproducto =$(this).parent().parent().children(".codigoproducto").html();
///before la asigna al html
    $(".agregarproducto").before(' <tr class="trjson"><th class="codigoproducto">'+codigoproducto+'</th></tr>');


    $(".eliminardetalle").one('click', function() {
////recoge variable del html que fue asigando del before
    $(this).parent().parent().remove();
    codigoproducto=$(this).parent().siblings(".codigoproducto").html();
    console.log(codigoproducto);

    });


    });
    
asked by Alberto Julio Arce Escolar 31.01.2018 в 17:25
source

1 answer

3

Like this:

$(".agregar,.otroselector,#yotromas").click(function(){
});

NOTE EDIT: Rereading what you ask, I think I have answered something that is not, I leave it anyway in case if it is what you ask. In the other case, it would be like this:

$("body").on( "click", ".agregar", function() {
});

With this method, even if the DOM is loaded, it will send the event to the selector element and you save generating events within events.

    
answered by 31.01.2018 / 17:48
source