For a website I'm doing, I want to load one div, and hide another, then have two buttons that will toggle views between the div using JavaScript.
This is my current code
<script type="text/javascript">
function replaceContentInContainer(target, source) {
document.getElementById(target).innerHTML = document.getElementById(source).innerHTML;
}
function replaceContentInOtherContainer(replace_target, source){
document.getElementById(replace_target).innerHTML = document.getElementById(source).innerHTML;
}
</script>
Current HTML:
<html>
<button onClick="replaceContentInContainer('target', 'replace_target')">View Portfolio</button>
<button onClick="replaceContentInOtherContainer('replace_target', 'target')">View Results</button>
<div>
<span id="target">div1</span>
</div>
<div style="display:none">
<span id="replace_target">div2</span>
</div>
The second function that replaces div2 is not working, but the first one is.
Just Simple Set the style attribute of ID:
To Show the hidden div
To hide the shown div
You can simply manipulate the style of the div in question...
How to show or hide an element:
In order to show or hide an element, manipulate the element's style property. In most cases, you probably just want to change the element's
display
property:Alternatively, if you would still like the element to occupy space (like if you were to hide a table cell), you could change the element's
visibility
property instead:Hiding a collection of elements:
If you want to hide a collection of elements, just iterate over each element and change the element's
display
tonone
:Showing a collection of elements:
Most of the time, you will probably just be toggling between
display: none
anddisplay: block
, which means that the following may be sufficient when showing a collection of elements.You can optionally specify the desired
display
as the second argument if you don't want it to default toblock
.Alternatively, a better approach for showing the element(s) would be to merely remove the inline
display
styling in order to revert it back to its initial state. Then check the computeddisplay
style of the element in order to determine whether it is being hidden by a cascaded rule. If so, then show the element.(If you want to take it a step further, you could even mimic what jQuery does and determine the element's default browser styling by appending the element to a blank
iframe
(without a conflicting stylesheet) and then retrieve the computed styling. In doing so, you will know the true initialdisplay
property value of the element and you won't have to hardcode a value in order to get the desired results.)Toggling the display:
Similarly, if you would like to toggle the
display
of an element or collection of elements, you could simply iterate over each element and determine whether it is visible by checking the computed value of thedisplay
property. If it's visible, set thedisplay
tonone
, otherwise remove the inlinedisplay
styling, and if it's still hidden, set thedisplay
to the specified value or the hardcoded default,block
.And the Purescript answer, for people using Halogen:
If you "inspect element", you'll see something like:
but nothing will actually display on your screen, as expected.
You can Hide/Show Div using Js function. sample below
HTML -