0

I have a button which is enabled at the beginning, but I want to disable it. I need it for a game. If the player chooses to play more, the game starts again, if not, the button "Click on me" should be disabled. Thank you in advance!

Here is my code:

    var y;
    function playAgain()
    {
        y=confirm("PLAY AGAIN?");

        if(y==true)
        {
            alert("Let's play it again!");
            location.reload(true);
        }
        else if(y==false)
        {
            alert("Thank you!\nSee you soon!");
            document.getElementById("button").disabled="disabled";
            //document.getElementById("button").disabled="true";
        }
    }

The HTML code:

<body>
    <input type="button" value="Click on me" onClick="playAgain()"  />
</body>
1
  • 1
    I think you forgot to add id="button" to your input button? Commented Sep 29, 2012 at 0:38

3 Answers 3

4

This will work:

document.getElementById("button").disabled = true;

If your button actually has id="button":

<input id="button" type="button" value="Click on me" onClick="playAgain()"  />

To re-enable the button set .disabled = false;

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

Comments

2

You're using document.getElementById("button") but there is no element named button in the HTML.

<input type="button" id="button" value="Click on me" onClick="playAgain()"  />

Also, to disable a button, set its disabled attribute to true (false for otherwise).

document.getElementById('button').disabled = true;

Comments

0

with jquery you can say

$("[type=button]").click(function(){
    $(this).attr("disabled","disabled");
    ... your other code ...
});

1 Comment

It's generally best to not drag in another library -- although this would work (because of the selector) -- as a primary fix. Take time to address why the current code doesn't work and then, if wanted, as secondary information, explain how it could be done using other means. Sometimes the other means are the correct way (although arguably not here), but always, always, focus/explain the primary issue in an answer and how the recommend approach fixes/addresses the problem.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.