0

I am trying to change the attribute "hidden" of the element to true by javascript after a timeout of 1,000 millisecond but it is not changing the attribute. Yes the timeout is working fine I've tested that, all functions are correct! Only the last part in which by using the setAttribute() function is not working.

HTML:

<div id="gamePanel" hidden="true">
    <div id="bb">
        <div id="map">

        </div>
    </div>
    <p id="status"> Welcome Back! </p>
    <input type="button" id="changeBio" value="Change Map" />
    <input type="button" id="saveGame" value="Save Game" />
    <div id="statusBar"> Status </div>
</div>

Javascript:

var panel = document.querySelector("#gamePanel");
panel.setAttribute("hidden","false");
2
  • Are you by any chance trying to change this HTML5 attribute in an older version of IE (or in quirks mode)? Commented Apr 15, 2014 at 18:54
  • @scunliffe No, I am using Google Chrome Commented Apr 15, 2014 at 18:55

2 Answers 2

3

Use

panel.removeAttribute("hidden");

Since hidden attribute is a boolean attribute, (the spec defines it this way),

The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

Then, it doesn't have true and false values. It can be present (that is, hidden or hidden="hidden"), or not.

Or, even better, you can change the hidden property:

panel.hidden = false;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for this simple solution, it works, but it messed up my CSS styling :S
1

Personally I would use this instead:

var panel = document.getElementById("gamePanel");
panel.style.display = 'block';

or to hide it:

panel.style.display = 'none';

2 Comments

This is much cleaner and backwards compatible. There have been issues when using both the hidden attribute and display to determine true visibility of items (I recall a WebDriver bug specifically back in August 2013)
hidden attribute is semantic. display:none is presentational. The spec explains the differences.

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.