/    Sign up×
Community /Pin to ProfileBookmark

Expand/Collapse Div Help

I am totally new to Javascript and I currently have a code I am using to expand a div on click and then collapse it on click. However, I need to change/add to it so that when one div is clicked the others that are open collapse (i.e., only one div can be open at a time). I have no idea how to modify the code so that it does this – I tried using a canned code off the internet but it messed up with the accordion effect of the content. There is also an error message but I am not sure what is causing that, either.

Here is the page (the 3 sections that expand/collapse are the left side navigation – so only one of them should be open showing the sublinks at a time): [url]http://www.retoldstory.com/products/products.html[/url]

Here is the code that I am using:

function expcoll(div) {
var el = document.getElementById(div);
if ( el.style.display == “none” ) {
el.style.display = ”;
}
else {
el.style.display = ‘none’;
}
}

Thank you for your help!! I really appreciate it ?

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@KDLAAug 15.2008 — FYI - your code WORKS perfectly in Firefox; it looks like IE is the only problem.
Copy linkTweet thisAlerts:
@virginiagirlauthorAug 15.2008 — That is weird! It is giving me an error in Firefox, Firebug gives me this explanation:

It says the error is coming from prototype.lite.js (from Mootools) and this is the error:

element has no properties

find(null, "nextSibling")prototype.lite.js (line 96)

(no name)(div.tab, 9)expcoll.js (line 16)

each(function())moo.fx.pack.js (line 216)

init()expcoll.js (line 15)

[Break on this error] while (element.nodeType != 1) element = element[what];

I googled the error but I couldn't find anyone else with it.
Copy linkTweet thisAlerts:
@KDLAAug 15.2008 — OK -- you may want to look at these errors: http://validator.w3.org/check?verbose=1&uri=http%3A%2F%2Fwww.retoldstory.com%2Fproducts%2Fproducts.html

The main issue is that you are repeating ID's in your code. ID's can used only once. If you need to repeat a usage of a style, then you need to employ a "class."

This could be part of the problem with your javascript. You are referencing an ID that is found several times within the code.

KDLA
Copy linkTweet thisAlerts:
@virginiagirlauthorAug 15.2008 — I see the IDs you are talking about - I think those are leftover from another Expand/Collapse function I was trying to use. I just removed them and updated the page. I don't know what is causing the error because I am using the same code on other pages that don't have this function and I still get the error message. It doesn't seem to be affecting the way the code is working but I hate seeing the error message!
Copy linkTweet thisAlerts:
@KDLAAug 15.2008 — HA HA -- Now it's not working in Firefox. At least the behavior is now consistent. :p
Copy linkTweet thisAlerts:
@virginiagirlauthorAug 15.2008 — I think that is lazy browser behavior ;-) Sometimes it doesn't display an error in IE and then when I reload the page the error returns. I think IE, at least, must get lazy and decide to show an error in the status bar.
Copy linkTweet thisAlerts:
@virginiagirlauthorAug 15.2008 — Yay, the function issue has been fixed. Now I have no idea how to fix the error message in the browser...
×

Success!

Help @virginiagirl 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.20,
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,
)...