0

I want to change the style:display property (none or block) of my div (displaydiv) according to the value of a global variable (disp). I want to check the value of this variable on page load and set the styel: display according to the value disp.

i set the value of disp as "none" in javascript.

i want to change the value within HTML TAG

But this div is always visible. Please help me

1
  • 2
    Note: please accept the answer that helped you out (if at all). It provides credit to the one who gave the right answer. Commented Sep 21, 2010 at 13:43

3 Answers 3

3
<script>
function hidemydiv() {
 if(disp == 'none') document.getElementById('displaydiv').style.display = 'none';
}
</script>

<body onload="hidemydiv()">
<div id="displaydiv">
    Lorem ipsum dolor sith amet
</div>
</body>

I just wonder what kind of global variable disp is. Is it in javascript? PHP? Where/when/how do you set it?

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

Comments

1

We can't tell without code but it sounds very much like you're executing your change-style JS inline before the target DIV has loaded.

Does your browser report errors on the page?

Is your JS code bound to the onload event somehow?

Is the style actually applied but overidden (check with firebug)?

Comments

1

Attach a function to the window onload event.

window.onload = function () {
    var elem = document.getElementById(divId);

    if (disp === "none") {
        elem.style.display = "none";
    }
    else {
        elem.style.display = "block";
    }
};

Comments

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.