How to autoclick a button (stimulate a mouse click on load)?

Viewed 1024

I am taking a website design class and my first project is to stimulate a mouse click for an audio player when the page loads.

My website is split in 3 files: HTML, CSS and JS.

The current code is:

    function onPlayerReady(event) {
        var playButton = document.getElementById("play");
       playButton.addEventListener("click", function() {
       player.playAudio(); 
     }); 

      var pauseButton = document.getElementById("pause");
      pauseButton.addEventListener("click", function() {
      player.pauseAudio();  
     });
    }
    <div class="buttons">
      <svg class="button" id="play">
        <use xlink:href="#play">
      </svg>
      <svg class="button" id="pause">
        <use xlink:href="#pause">
      </svg>
    </div>

2 Answers

DOM elements have a click function that allows you to do just that.

document.onload(() => document.getElementById('play').click());

With help Of .click() you can fire manually button click method. So in Your Case, if you want to auto click play button then you have to write


    // this function will execute automatically when page  all contents are ready/loaded 
    $(document).ready(function(){
    //add click event lister
    onPlayerReady(event);
    //auto click button with help of
    playButton.click();
    });
Fore More detail you can find jquery document

Related