Forum Moderators: open
I am trying to write some code so that when you click on the <h3>, it finds the nextSibling (which is always a div) and then shows or hides it based on whether it's already shown or hidden. It works great in IE but it won't work in Firefox
The HTML code looks a little like this:
<h3>Header</h3>
<div>
<p>Content</p>
<p>More Content</p>
</div>
There are two DOM functions - the first one finds the div tags and sets them accordingly with an onclick = function() embedded. The second one looks like this:
function createHideShow(thislist) {
var pArea = thislist.nextSibling;
// Displays the <p> and <img> tags if they are hidden
if (pArea.style.display == "none") {
pArea.style.display = "block";
}
else {
pArea.style.display = "none";
}
}
Any ideas?
<h3>Header</h3><div>
In that case, the <div> will be the next sibling. However, I would recommend that instead you do something more like this:
function createHideShow(thislist) {
for( pArea = thislist.nextSibling; pArea; pArea = pArea.nextSibling )
{
if( pArea.nodeName.toLowerCase() == 'div' )
{
// Displays the <p> and <img> tags if they are hidden
if (pArea.style.display == "none") {
pArea.style.display = "block";
}
else {
pArea.style.display = "none";
}
break;
}
}
}
Sorry, I have a *thing* for one-liners. :-)
I always see this as an attribute of the display property, why doesn't anyone use the visibility property?
pArea.style.visibility = (pArea.style.visibility == "visible")?"hidden":"visible";