Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a complete, cross-browser, and extensible option. Look at the comments in the code for the description. Feel free to use this in any projects you like.</p> <p><strong>CSS:</strong></p> <pre><code>&lt;style type="text/css"&gt; a { text-decoration: none; outline: none; } div.TogWrap { width: 400px; padding: 12px; } /* classes .on and .off are for the links */ .off { border: 1px solid #bebebe; padding: 7px 8px; background: #df7623; color: #fff; } .on { border: 1px solid #bebebe; padding: 7px 8px; background: #bf7623; color: #fff; } /* classes .hide and .show are used for the content */ .hide { display: none; } .show { display: block; margin-top: 8px; border: 1px solid #bebebe; padding: 16px 10px 10px 10px; background: #ededed; } &lt;/style&gt; </code></pre> <p><strong>JavaScript:</strong></p> <pre><code>&lt;script type="text/javascript"&gt; /* Cross-Browser Event functions (required) */ function addEvent (eventObj, event, codeToExecute) { if (eventObj.addEventListener) { eventObj.addEventListener(event, codeToExecute, false ); return true; } else if (eventObj.attachEvent) { event = "on" + event; eventObj.attachEvent(event, codeToExecute); } else { eventObj['on' + event] = codeToExecute; } } function cancelEvent(event) { if (event.preventDefault) { event.preventDefault(); event.stopPropagation(); } else { event.returnValue = false; event.cancelBubble = true; } } /* The function that does the hide/show */ function toggleIt (thisEl, thisContent) { var el = document.getElementById(thisEl); var content = document.getElementById(thisContent); content.className = "hide"; //initially hide the contents el.className = "off"; //and set the links class to off (optional) var toggle = function (event) { //capture the event that was triggered switch (event.type) { //check if it was a click event case 'click': //if click content.className = content.className === 'hide' ? 'show' : 'hide'; //self explanatory el.className = content.className === 'hide' ? 'off' : 'on'; //self explanatory (optional) break; } cancelEvent(event); //prevent the link from following the href attribute }; addEvent (el, 'click', toggle); //onclick call the toggle function } /* Set up function */ function initToggles () { //Array of IDs for the links that are clicked - add as many as you need var allTriggers = [ 'togTrigger1', 'togTrigger2' ]; //Array of IDs for the content that you want to hide/show - add as many as you need var allContents = [ 'content1', 'content2' ]; var i = 0, arrLen = allTriggers.length; for (i; i &lt; arrLen; i += 1) { toggleIt(allTriggers[i], allContents[i]); } } /* the same as window.onload */ addEvent (window, 'load', initToggles); &lt;/script&gt; </code></pre> <p><strong>HTML:</strong></p> <pre><code>&lt;!--You can add as many of these as you need. Just follow the same pattern as I have below --&gt; &lt;div class="TogWrap"&gt; &lt;a href="#" id="togTrigger1" class=""&gt;Open it / Close it&lt;/a&gt; &lt;p id ="content1" class="togContent"&gt; Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Mauris magna. Suspendisse accumsan elit non tellus. Curabitur eros justo, malesuada convallis, sagittis vitae, convallis sit amet, lectus. &lt;/p&gt; &lt;/div&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;!--Here is another one following the same pattern--&gt; &lt;div class="TogWrap"&gt; &lt;a href="#" id="togTrigger2" class=""&gt;Open it / Close it&lt;/a&gt; &lt;p id ="content2" class="togContent"&gt; Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Mauris magna. Suspendisse accumsan elit non tellus. Curabitur eros justo, malesuada convallis, sagittis vitae, convallis sit amet, lectus. &lt;/p&gt; &lt;/div&gt; </code></pre> <p>I hope it helps! :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload