/    Sign up×
Community /Pin to ProfileBookmark

toggle function not working when being called from another function

Hi,

I have a toggle function that works brilliantly

[code]
// Toggle
function toggleDiv(elementshow, element, elementhide, elementhide2, elementhide3){
var els;
var newDisplay;

elh = document.getElementById(elementhide);

// if the elh does not exist then hide the show one otherwise show and hide the relative ones
if (elh) {

els = document.getElementById(elementshow);
elh2 = document.getElementById(elementhide2);
elh3 = document.getElementById(elementhide3);

if (els.style.display != ‘block’) {
els.style.display = ‘block’;
}

if(elh.style.display != ‘none’) {
elh.style.display = ‘none’;
}

if ((elh2) && (elh2.style.display != ‘none’)) {
elh2.style.display = ‘none’;
}

if ((elh3) && (elh3.style.display != ‘none’)) {
elh3.style.display = ‘none’;
}
} else {

// Get the elements that have the tag of els
var allelements = getElementsByClass(elementshow, null, element);

for(var i = 0; i < allelements.length; i++) {
if (allelements[i].style.display != ‘block’) {
allelements[i].style.display = ‘block’;
} else {
allelements[i].style.display = ‘none’;
}
}
}
}
[/code]

This works without a problem and I will run it like so

[code]
onclick=”toggleDiv(‘gal’,null,’img’);”
[/code]

What I am trying to do this is run it without the onclick from within a javascript function
I basicly have this function

[code]
// Trigger onclick
function triggerOnClick(inputName, inputValue) {
toggleDiv(‘gal’,null,’img’);
}
[/code]

As you can see at the moment I am just trying to run that togglediv function.

Problem is it is not correctly running it? Nothing happens when the page loads.

I am running it like so

[code]
<script language=”javascript” type=”text/javascript”>
triggerOnClick();
</script>
[/code]

Why is the toggle not correctly working but it is when I click on it?

Thanks
k0r54

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@k0r54authorJan 12.2010 — Ok,

I think the issue with my actuall toggle function because i I manually type
<i>
</i>toggleDiv('img',null,'gal');

on the html page it does nothing. It seems to ignore me calling that function.

??
Copy linkTweet thisAlerts:
@iandevlinJan 12.2010 — Run it in Firefox and use the Tools->Error Console and see what JS errors you get.
Copy linkTweet thisAlerts:
@k0r54authorJan 12.2010 — Ok,

Thats for your tip I will bare that in mind for the future. I actually stumpled upon this with my testing when I place the function in the body onload and realised i think it because there is no trigger
<i>
</i> window.onload = function() { toggleElement('img',null,'gal') }


This appears to have resolved my issue - hopefully ?
Copy linkTweet thisAlerts:
@skywalker2208Jan 12.2010 — Run it in Firefox and use the Tools->Error Console and see what JS errors you get.[/QUOTE]

Even better would be to install firebug and look at the errors through firebug.
×

Success!

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