1

Hi this is an odd question and i will try to ask it correctly. I have a function using javascript called load canvas.

function loadCanvas(canvas) {

relevant code here...

} 

I also have a normal button called btn.

<button class="btn" type="button">Play!</button>

I am wondering can i disable the function until the play button is selected? The function is for a game using javascript. So on load there isnt anything there until i press play then it appears!

any ideas/help please?

8
  • Call loadCanvas on click of this play button ? Commented Jan 25, 2016 at 12:47
  • @AmitJS94 Yes kind of like that, at the moment i have it loading straight in as the page loads up. I want to disable until the button is selected Commented Jan 25, 2016 at 12:48
  • It's a function. It isn't going to do anything until some other code calls it. So don't call it until you want it to run. Commented Jan 25, 2016 at 12:49
  • Some other function is calling loadCanvas, that's why it's executing. You can debug this by checking the stack in the console to see which function is calling loadCanvas. Commented Jan 25, 2016 at 12:49
  • Like @AmitJS94 said you need to call function while play button click. Commented Jan 25, 2016 at 12:51

4 Answers 4

1
$(document).ready(function(){

    var loadCanvas=  function (canvas) {

             relevant code here...

         } 

    $("#test").on('click',loadCanvas()); // using jquery

document.getElementById("test").addEventListener("click",loadCanvas()); // using javascript

<button class="btn" id="test" type="button">Play!</button>

})
Sign up to request clarification or add additional context in comments.

Comments

1

If you are having issue because other method is triggering the function you can add a flag with a boolean and turn it on when you click..

Something like that:

The button don't change at all

<button class="btn" type="button">Play!</button>

The js code with this change:

var buttonClicked = false;
function loadCanvas(canvas) {
    if(buttonClicked){
        relevant code here...
    }
} 

And in the on click function add this before call the function:

buttonClicked = true;

At the end your js should look like this:

var buttonClicked = false;
function loadCanvas(canvas) {
    if(buttonClicked){
        relevant code here...
    }
} 

$(".btn").click(function(){
    buttonClicked  = true;
    var canvas = ...;
    loadCanvas(canvas );
});

EDIT

If you have more buttons with the class .btn you should use an id and change the selector of the .click() with the selector of the id instead of the class selector

Comments

0

As you mentioned in the comments <script type="text/javascript"> loadCanvas("game"); </script> You are calling the function as soon as the page loads. So you will have to change it to:

<button class="btn play-button" type="button">Play!</button>

    <script type="text/javascript">
         $('.play-button').click(function(e){
        loadCanvas("game");}
        );
    </script>

If you are not using jquery you will have to handle the click event by javascript.

Comments

-1

You got to do following:

function loadCanvas(game) {
	alert('loading canvas');
}
<button class="btn" type="button" onclick="loadCanvas('game')">Play!</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.