Clicking on one button to trigger click event on another

后端 未结 3 657
被撕碎了的回忆
被撕碎了的回忆 2021-01-18 01:46

I want to click on button 2 to trigger a click event on button 1.

However, when I try the following, nothing happens when clicking on #2: no alert for #1 or #2.

3条回答
  •  失恋的感觉
    2021-01-18 01:49

    When you need to call click event of any button then syntax is as below

    $('selector').trigger(event);
    

    selector -> which tag's event you need to fire

    event -> which event you need to fire

    In your case you need to change following one line

    $('#button-1').trigger(e);
    

    here you need pass event of e like click event, select event, etc.

    so solution is replace this line with any of the following line

    $('#button-1').trigger(e.type); 
    
    $('#button-1').trigger("click");
    

提交回复
热议问题