Javascript: I want this game i made to start when the user clicks a button on the website . what is wrong here

$(document).onclick(function(){ //game starts

if(!started){

  $("#level-title").text("level "+level);//changes levels as game progresses

  nextSequence();

  started=true;

}

});

There really is not enough context here to help you. If you have your code in github (or something similar) then if you give us the link to your code we can take a better look.

But I will say that you have the click handler on the document which means that this handler will be called when the user clicks on anything on the page, not just a certain button. If you want to limit this to just a certain button then you should add this click handler to the button.

Also, it appears that you are using jQuery? If so then it is click() not onclick().

1 Like