Hi there!
I am doing the Simons game, and I would like to know how can I make a button click itself using a function?
After this function is done I want to use it into another function to make buttons be clicked randomly.
I already have all the buttons effects in which on a click it plays the song and changes its colours declared in a variable such as `` var blueButtonEffect = code here;```.
1 Like
Not sure if this is what you are looking for.
HTMLElement.click() or Jquery .click()
https://api.jquery.com/click/
2 Likes
Just to clarify for those who may have the same issue. You don’t really need a function to make a button click itself. As @lasjorg mentioned above you just need the .click() method. Using plain JS it would looks like this document.getElementById('#example').click();
or using jQuery $('example).click()
.
However, if you need to use a function to make a button to click by itself randomly I did like this.
var blueButtonEffect = code effect here;
var redButtonEffect = code effect here;
var greenButtonEffect = code effect here;
var yellowButtonEffect= code effect here;
var arr = [blueButtonEffect, redButtonEffect, greenButtonEffect, yellowButtonEffect];
//will click on buttons randomly
function clickByItself(){
let random = Math.floor(Math.random()*arr.length)
$(arr[random]).click();
}
clickByItself();
like this, every time I reload the page a different button is clicked using the Math.floor(Math.random()*var.lenght) method.
Of course, there are very easy and smart ways to do that but as a beginner like me it works.
Now I just need to understand how to loop this effect 