I have a button at the bottom of my page that I hide and unhide by setting its style to display:none and display:block respectively. I do this with many objects multiple times throughout my code, so I use the following function:
function objectHide(object, hide) {
if (hide == 'hide') {
document.getElementById(object).style.display = "none";
}
else if (hide == 'unhide') {
document.getElementById(object).style.display = "block";
}
}
Once the button has been hidden and unhidden once it is no longer aligned to the center of the page and defaults to the standard left side.
Text-align center is attached to the body, but I have also tried attaching it to the button’s parent div, both in CSS and in Javascript after it is unhidden. None of these have worked and it continues to change back to the standard left side.
3
Answers
If you really need to use
.display
for this then you’ll have to set the style property again when unhiding. Otherwise, you can toggling the.visibility
property of your UI element. This should accomplish what you’re trying to achieve here.I hope this helps.
It’s better to put the hide function in the button’s parent element. Something like this:
HTML
CSS:
For the JavaScript:
From the MDN See Also section for
initial
:Using
revert
means you don’t have to determine what an element’sdisplay
attribute should be to re-display the item.Note the code snippet techniques of using the variables created for element ids is for brevity and not recommended in production (they may be shadowed by global variables or functions of the same name).