
Published in Website Design on Saturday, 21 February 2009 |
Post RSS
This simple script uses a link to show and hide an area of HTML on a web page.
You can set the layer to be visible or invisible when the page first loads. When the link is clicked, the JS toggles CSS visibility of the layer between 'visible' and 'none'.
The text of the link changes to reflect the status of the HTML area.
Include the following functions in the <head> area of the page (or include them in your external javascript file):
<script type="text/javascript">
function toggleLayer(layerId, linkid, linktoshow, linktohide){
if(document.getElementById(layerId).style.display == "none"){
// div is hidden, so let's show
document.getElementById(layerId).style.display = "block";
document.getElementById(linkid).innerHTML = linktohide;
}else{
// div is not hidden, so hide
document.getElementById(layerId).style.display = "none";
document.getElementById(linkid).innerHTML = linktoshow;
}
}
</script>
Use this code in the page <body> to activate the function:
<a href="javascript:void()" id="showHideLink" onClick="toggleLayer('showHide', 'showHideLink', 'Show Layer', 'Hide Layer');">Show Layer</a>
<div id="showHide" style="display:none;">
<h3>Show / Hide Area</h3>
<p>Put the HTML to show and hide in this div. It can include links, images, forms and nearly all regular HTML elements!</p>
</div>
Note: The initial CSS style - display:none; - needs to be included inline in the HTML (as opposed to an external CSS file).
Whatever your business needs, one of our highly trained small business development experts is sure to be able to help.
web design nottingham, website design for small business, javascript, layers
Welcome to WebFertile. We'd love you to join us and benefit from:
View project briefs, client comments + latest Google® performance.
WebFertile
The Hive Business Centre
NTU Buildings
Burton Street
Nottingham
NG1 4BU
0115 849 2660
07973 122 619