I have a div that needs to be hidden by default. It then can be toggled by a button:
<script type="text/javascript">
function toggle() {
text = document.getElementById('add_view');
var isHidden = text.style.display == 'none';
text.style.display = isHidden ? 'block' : 'none';
}
$(document).ready
(
function () {
toggle();
$("#add_view, #btnToggle").click(function (e) {
e.stopPropagation();
});
$(document).click(function () {
toggle();
});
}
);
</script>
It is working fine. The only problem is, when I refresh the page, I momentarily see the div before it is hidden.
What could I do to prevent that?
Thanks
I have a div that needs to be hidden by default. It then can be toggled by a button:
<script type="text/javascript">
function toggle() {
text = document.getElementById('add_view');
var isHidden = text.style.display == 'none';
text.style.display = isHidden ? 'block' : 'none';
}
$(document).ready
(
function () {
toggle();
$("#add_view, #btnToggle").click(function (e) {
e.stopPropagation();
});
$(document).click(function () {
toggle();
});
}
);
</script>
It is working fine. The only problem is, when I refresh the page, I momentarily see the div before it is hidden.
What could I do to prevent that?
Thanks
Share Improve this question edited Feb 5, 2013 at 19:06 Rui Jarimba 18.2k11 gold badges64 silver badges98 bronze badges asked Feb 5, 2013 at 19:01 user2043533user2043533 7513 gold badges9 silver badges23 bronze badges 1- don't forget to mark one of the answers as correct if you're happy with any – Rui Jarimba Commented Feb 5, 2013 at 20:04
3 Answers
Reset to default 10You probably need to hide your element by default, and then use the button to toggle visibility. Try this:
<div id="add_view" style="display:none">....</div>
Make the element hidden in your html to begin with.
<div id="add_view" style="display: none;"></div>
Initially, you have to hide it by setting style="display:none;"
of the div. Once when u want to toggle it, you have to use it as
document.getElementById(Id).style.display="";
in javascript.