New Site Intro Using Toggle Hidden DIV

In my spare time it’s been my new hobby to re-learn HTML and CSS (post coming up soon regarding it). I’m using RyanEagle.com as my “testing” ground, risking little. Yesterday I managed to make all the mobile menus disappear on my main site, you could say I’m still quite the beginner. It was time to write an updated intro copy for my website and wanted to experiment with hiding & displaying <div>‘s – perfect opportunity. My goal was to create a concise introduction that briefly introduces who I am, without overwhelming people with information. I achieved this perfectly with the newfound code I’ll share with you today, Below a preview of the introduction located directly on the Ryan Eagle Homepage, click the image to see the interactive (and mobile friendly) introduction.
Using Toggle DIV

How To Toggle Hide <div>’s:

To achieve the toggle hiding div effect I used some pretty simple code – a combination of Javascript, HTML5 and CSS3. First, start by adding the Javascript to your webpage:

<script type=”text/javascript”>
function toggleMe(a){
var e=document.getElementById(a);
var i = document.getElementById(a + ‘_image’);
if(!e)return true;
if(e.style.display==”none”){
e.style.display=”block”
i.src = ‘TOGGLE_IMAGE_1.GIF’;
} else {
e.style.display=”none”
i.src = ‘TOGGLE_IMAGE_2.GIF’;
}
return false;
}
</script>

Now, for the hidden <div>:

<div id=”YOUR-ID-HERE” style=”display:none”>Any Content Here Will Be Hidden On Default</div>

Now, for the link code. Before continuing, you don’t need to restrict the use of this Javascript code to text – you can use this function to make everything from <div>‘s, to <li>‘s, to <img>‘s clickable:

<span style=”cursor:pointer;” onclick=”return toggleMe(‘YOUR-ID-HERE’);”>Clickable Text, You May Want To Consider Adding An Underline or Other Effects so Visitors Know The Element Can Be Clicked</span>

Ta’da, you’re done. That was pretty painless wasn’t it? Now this may not be as cool for you as it was for me, but getting it to work was quite the accomplishment for me. Let’s say the last time I hard coded without the assistance of Dreamweaver was before HTML5 was released.