Home > Back-end >  Click two elements at the same time by clicking one button with javascript
Click two elements at the same time by clicking one button with javascript

Time:02-11

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

CodePudding user response:

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>

CodePudding user response:

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 >Do something here</button>
   <button >And here</button>
   <script>
     const button = document.getElementsByClassName ('button');
     button.addEventListener('click', () => {
     // do whatever you want here
        });
   </script>
  • Related