/    Sign up×
Community /Pin to ProfileBookmark

Simple Collapse/Expand Style Manipulation Problem

I have a javascript method I quickly wrote:

[CODE]
function openCloseTable(elementID) {
var setElement = document.getElementById(elementID);
if (setElement.style.display == ‘none’)
setElement.style.display = ‘block’;
else
setElement.style.display = ‘none’;
}
[/CODE]

I’m just trying to have the <div> container expand and collapse cleanly and efficiently, with minimal code.

I call the function like this:

[CODE]<a href=”JavaScript:openCloseTable(‘trainingContainer’)”>Training Objectives</a>[/CODE]

My problem occurs the first time you click on the link. It always never expands. However, if you click a second time, the <div> expands fine and collapses fine. It’s just that darn first click.

Any help is greatly appreciated.

Rez

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@MongusDec 21.2005 — Just change this line: if (setElement.style.display == 'none')
to: if (setElement.style.display != 'block')

The problem is unless you've explicitly set the value in JavaScript it is undefined in JavaScript even though you've set it via CSS.
Copy linkTweet thisAlerts:
@RezonauthorDec 21.2005 — Thanks a ton! ? That did it.

I appreciate the explanation as well.
×

Success!

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