'Click two elements at the same time by clicking one button with javascript

Is it possible to click one button and simultaneously click another element. Like I click a button to show an image and simultaneously let something else appear which would also appear when clicking on it



Solution 1:[1]

It's totally possible. You have to get the click event of the element, and you can do whatever you want.

Example:

  <button id="button">I am a button</button>
  <div id="anotherElement"></div>
  <script>
    const button = document.getElementById('button');
    const anotherElement = document.getElementById('anotherElement');
    button.addEventListener('click', () => {
      // do whatever you want here
      anotherElement.click();
    });
  </script>

Solution 2:[2]

As our friend Said says, is possible, just I think if would be better if you name classes instead of ID because is a better practice

   <button class="button">Do something here</button>
   <button class="button">And here</button>
   <script>
     const button = document.getElementsByClassName ('button');
     button.addEventListener('click', () => {
     // do whatever you want here
        });
   </script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Said Torres
Solution 2 Luis Olivárez