Search code examples
javascriptonclickdom-eventsonclicklistenerbuttonclick

Propagate click event on multiple elements in the same place but not have parent-child relationship


I have a question that how can I propagate a click event on 2 elements in the same place but it doesn't have parent-child relationship. For example, I have this html:

<body>
    <button id="btn1">BUTTON 1</button>  // position fixed with top/left at 10px
    <div>
        <button id="btn2">BUTTON 2</button>  // position fixed with top/left at 10px (same place of button 1)
    </div>
</body>

So in the UI, button 1 and button 2 nearly have the same place, so I want if I click on button 2, the click event is also fired on both button 1 and button 2.

I appreciate any help.


Solution

  • You have to listen to the click event on your button 2 and then call the click event for button 1 as well.