Forum Moderators: open

Message Too Old, No Replies

Multiple hide/show divs, both using images

Trying to create 2 independent divs, both using images as show/hide links

         

floydboy

2:22 am on Sep 30, 2010 (gmt 0)

10+ Year Member



Hi. I'm trying to create a page that has two (completely independent) divs that are hidden at page load, and both use images for their show/hide links. I have code that uses an image as a link to show/hide a div, but when I tried to use it twice in the same page just by changing the id's, they both stopped working. I put this in the <head>...

<script language="javascript">
function toggle5(showHideDiv, switchImgTag) {
var ele = document.getElementById(showHideDiv);
var imageEle = document.getElementById(switchImgTag);
if(ele.style.display == "block") {
ele.style.display = "none";
imageEle.innerHTML = '<img src="example.png">';
}
else {
ele.style.display = "block";
imageEle.innerHTML = '<img src="example.png">';
}
}
</script>


...and this in the <body>...

<div id="headerDivImg">
<a id="imageDivLink"
href="javascript:toggle5('contentDivImg','imageDivLink');"<img src="example.png"></a>


Sorry I'm such a novice... How can I make this work for two different divs in the same page? Thanks!

whoisgregg

12:56 pm on Sep 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, floydboy!

I see a syntax error that might be the cause of the trouble (or maybe just a copy/paste error).

The end of the opening <a> tag is missing:

<a id="imageDivLink"
href="javascript:toggle5('contentDivImg','imageDivLink');"<img src="example.png"></a>


Should be:

<a id="imageDivLink"
href="javascript:toggle5('contentDivImg','imageDivLink');"><img src="example.png"></a>



If that isn't the cause, could you perhaps post a bit more code, specifically the second toggle img?

floydboy

7:05 pm on Sep 30, 2010 (gmt 0)

10+ Year Member



That was a copy/paste error, sorry. And that's the only code there is, I would have thought it would work for 2 independent divs with 2 independent links as long as I changed the id's, but for some reason it isn't. Again, I'm a total novice, so somebody help me out here? Thanks anyway!