3

I have some buttons which have a hover effect which makes the button color darker on hover ( with css ).

When the user clicks on those buttons, i disable them via javascript.

when i want the procedure ( game ) to start again and i re-enable the buttons via javascript, the css button: hover effect does not work any more.

Any solutions?

the javascript enable/disable button functions are:

function jsEnableElement(id) {

if ( document.getElementById(id) ) {
    document.getElementById(id).style.background = "#3399FF";
    document.getElementById(id).style.cursor = "pointer";
    document.getElementById(id).disabled = false;
}

}

function jsDisableElement(id) {

if ( document.getElementById(id) ) {
    document.getElementById(id).style.background = "#DDDDDD";
    document.getElementById(id).style.cursor = "default";
    document.getElementById(id).disabled = true;
}

}
3
  • Can you show is the HTML for your button please ... ta Commented Dec 8, 2011 at 10:01
  • <button id="btnL1" type="submit" onclick="jsHangmanCheckLetter(id, 'a');"><span id="span1">a</span></button> Commented Dec 8, 2011 at 10:20
  • and function jsHangmanCheckLetter(sID, sLetter) { try { if ( g_GameIsActive === true ) { jsDisableElement(sID); } } catch(err) { alert("ERROR :: sHangmanCheckLetter(sLetter) :: " + err); } } Commented Dec 8, 2011 at 10:22

2 Answers 2

4

I'm not sure why the behaviour breaks when you change the background color of an element eventhough you keep its ID or Class name. But appearently it does.

So another way for you to fix this, and its perhaps a better way is to change the class of the element.

I don't know what element you want to disable, but i'm using a DIV as example. A working demo can be found here:

http://jsfiddle.net/yVVk6/

But just for completness i'll add the code in here aswell

<html>
<head>
    <script>
        function jsEnableElement(id) {
            if ( document.getElementById(id) ) {
                document.getElementById(id).removeAttribute("disabled");
                document.getElementById(id).className = "enabled";
                //document.getElementById(id).disabled = false;
            }
        }

        function jsDisableElement(id) {
            if ( document.getElementById(id) ) {
                document.getElementById(id).removeAttribute("enabled");
                document.getElementById(id).className = "disabled";
                //document.getElementById(id).disabled = true;
            }
        }
    </script>
</head>
<body>
    <div id="hallo" class="enabled" onclick="jsDisableElement('hallo');">Click me to disable</div>
    <div onclick="jsEnableElement('hallo');">Click me to enable it again</div>
</body>

CSS:

.enabled {
    background: #3399FF;   
}
.enabled:hover {
    background: #00FF66;
}

.disabled {
    background: #DDD;
}

EDIT: Just another thing i would like to mention is, if you go with this solution, then i really advise you to use a library like jQuery. It allows you to easily edit, delete or add classes to elements. In this example i simply delete all classes from the element.

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

1 Comment

Here is a pure JavaScript way to manipulate classes .... very simple .... stackoverflow.com/questions/195951/…
0

I think you can directly give the attribute as shown below:

document.getElementById("btnL1").setAttribute("disabled","true")   //to disable the button
document.getElementById("btnL1").removeAttribute("disabled")       //enable it again

1 Comment

I think you have misunderstood the question - the enable / disable works - the problem is that the css :hover attribute doesnt work after being enabled again

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.