/    Sign up×
Community /Pin to ProfileBookmark

Hide/Show Script

Hey guys,

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:

[code=html]<h3>Header</h3>
<div>
<p>Content</p>
<p>More Content</p>
</div>[/code]

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:

[code=html]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”;
}
}[/code]

Any ideas?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisJan 17.2007 — What is the initial state of pArea.style.display? It may be that it is not "none".
Copy linkTweet thisAlerts:
@mrhooJan 17.2007 — The next sibling is a text node, probably a new line or other whitespace, since IE doesn't see it.

var pArea=thisList.nextSibling;

while(pArea.nodeType!=1)pArea =pArea.nextSibling;

If you are going to show inline content like images I wouldn't set display=block.

Switch the display property from 'none' to the empty string, which will reset the element to its css default.
×

Success!

Help @TymArtist spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...