/    Sign up×
Community /Pin to ProfileBookmark

This code posted does NOT work for me. I have been trying countless hours now to be able to toggle a div. Of course it works perfectly in FireFox.

This is my code:

function togglediv(divid)
{
if(document.getElementById(divid).style.visibility == ‘hidden’)
{
document.getElementById(divid).style.visibility = ‘visible’;
}
else
{
document.getElementById(divid).style.visibility = ‘hidden’;
}
}

All this is in the <head> tag.

Then in the body I have this:

<? $user_agent = isset($_SERVER[‘HTTP_USER_AGENT’]) ? $_SERVER[‘HTTP_USER_AGENT’] : ”;

if(strpos($user_agent, ‘MSIE’) !== false)
{
echo “<input type=”button” value=”Show Hide Comments (“,$totalComments,”)”onClick=”javascript:toggle(‘comments’)” style=”margin: 0px 585px 50px 0px;” />”;
}

Which dispalys as:

<input type=”button” value=”Show Hide Comments (2)”onClick=”javascript:togglediv(‘comments’);” style=”margin: 0px 585px 50px 0px;” />

If you view source. It works fine in FireFox, can anyone help me make this work?

Thanks,

Shane

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jun 20.2008 — What about toggling the display between block and none. And what happens (or doesn't happen) in IE. Also:<i>
</i>&lt;input type="button" value="Show Hide Comments (2)" onclick="togglediv('comments');" style="margin: 0px 585px 50px 0px;" /&gt;
is better.
Copy linkTweet thisAlerts:
@webDevShaneauthorJun 20.2008 — What happens is nothing. The button displays, but when I click it I get an Object Expected error, and the line <input.... etc>

And I have no idea what mean you by "is better". There is no which is better. That is my code.
Copy linkTweet thisAlerts:
@Declan1991Jun 20.2008 — "javascript:" when used withing an onclick creates a label for the function call which is totally unnecessary, so leaving it out is better. Also having a space between attribute values and the next event handler or attribute is highly recommended.
Copy linkTweet thisAlerts:
@FangJun 20.2008 — Do you have other elements with the same [I]id[/I] or [I]name[/I]?
Copy linkTweet thisAlerts:
@webDevShaneauthorJun 20.2008 — No.

However some how I fixed it.

For some reason IE7 responds to this:

function togglediv(divid)

{

var div = document.getElementById(divid)

if(div.style.visibility == 'hidden')

{

div.style.visibility = 'visible';

}

else

{

div.style.visibility = 'hidden';

}

}

But, not what I had originally even though they are just syntactic sugar.
×

Success!

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