JQuery trigger click does not return target id

15 views Asked by At

I have the following jquery code to trigger a click event:

 $(document).on('click','#button1, #button2',function(e){
     console.log(e.target.id);
    
     if (e.target.id == 'button1') {
         $( "#widgetA .closeresults" ).trigger( "click" );
     }
     else {
         $( "#widgetB .closeresults" ).trigger( "click" );
     }
 });

The trigger event should fire the following code however e.target.id is always empty:

 $(document).on('click','#widgetA .closeresults, #widgetB .closeresults',function(e){
      console.log(e.target.id)
      if (e.target.id == 'widgetA') {
          //do something
      }
      else {
         // do something else
      }
      
 });   

How do i get the id of the element that initiated the trigger event?

*** UPDATE ***

If button1 is clicked then I want the trigger click event which calls the second snippet of code to return widgetA as the target.id as that is what I'm using to trigger the event. How can I achieve this.

0

There are 0 answers