/    Sign up×
Community /Pin to ProfileBookmark

Function help.. again

hey guys i have this function, i have hidden divs under each a:link in my nav and onclick it shows them sliding down while pushing the rest of the nav down and to close it i have to click the a:link again. Im wondering if there is something to add in my function that when you have one hidden div open it automatically closes when you click a second a:link so a user doesnt have more then one hidden div open at a time heres my function.

function toggle_div(div_id) {
var div = $(‘#’+div_id);
var divs = $(‘#main-nav’).children(‘div’).not(div);

if(div.css(‘display’) == ‘none’)
{
divs.slideUp(function() {
div.slideDown();
});
}
else
{
div.slideUp();
}
}

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@nicholasstephanOct 27.2009 — that should work. you've got all divs sliding up, then the selected on sliding down on the callback.

Another way to go about it would be to store the open element in a global variable, then use that to close it when another one is clicked. I haven't tested this code, but it's a direction:

[CODE]var open_div = null;

function toggle_div(div_id) {
var div = $('#'+div_id);
var divs = $('#main-nav').children('div').not(div);

if(open_div != div){
open_div.slideUp(function(){
open_div = div;
});
}

if(div.css('display') == 'none')
{
div.slideDown();
}
else
{
div.slideUp();
}
}[/CODE]
×

Success!

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