Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Toggle visibility script stopped working
Old 02-19-2010, 08:59 AM Toggle visibility script stopped working
sigdrifa's Avatar
Junior Talker

Posts: 3
Location: Germany
Trades: 0
Hi all,

I'm new here, so hi to everybody

I just ran into a surprising problem with a script to toggle the visibility of an element.
Here's the code I used:
Code:
function toggle(object) {
    var element = document.getElementById(object);
        if (element.style.display == 'block') {
        element.style.display = 'none';
        } else {
            element.style.display = 'block';
            }
        if (element.style.display == 'block');
}
Pretty standard I guess.

It used to work just fine the first time I set it up. Now I wanted to use it on another website, set everything up, but when I tested it, it toggled the visibility of the triggering link instead of the element with the specified id, e.g.:

Code:
<a href="#" onclick="toggle( 'someID' ); return false;">Toggle</a>
In the above example, instead of showing the element with the id "someID" the link text would be hidden after clicking on "Toggle".

Here's the page where I originally used this code:
http://www.kulturinsel-celle.de/?content=musiker
Clicking on any of the pictures or any of the "Biographie"-Links should show a hidden div containing some text. Clicking on that text div would toggle again and hide it.

When I first uploaded everything it worked as expected, and now it doesn't anymore, which is driving me nuts because I didn't change anything about it.

Tested with Firefox & Google Chrome on Linux and with IE8 on WinXP.

Ideas, anyone?

Thanks

Edit:
I just created this little html-file:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
  <title>Toggle</title>
  <meta name="GENERATOR" content="Quanta Plus">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <script type="text/javascript">
  // toggle visibility
function toggle(object) {
    var element = document.getElementById(object);
        if (element.style.display == 'block') {
        element.style.display = 'none';
        } else {
            element.style.display = 'block';
            }
        if (element.style.display == 'block');
}
  </script>
</head>
<body>
<div id="hide" style="display:none;">This Text is hidden.</div>
<a href="#" onclick="toggle( 'hide' ); return false;">Show/Hide.</a>
</body>
</html>
Everything works as expected.
On the page at the link above it's not working, although I don't find a difference in the JS part.

Last edited by sigdrifa; 02-19-2010 at 09:52 AM..
sigdrifa is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-27-2010, 05:45 AM Re: Toggle visibility script stopped working
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Call me crazy, but the page you link to doesn't have a single "onclick" in the source.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 02-27-2010, 05:56 AM Re: Toggle visibility script stopped working
sigdrifa's Avatar
Junior Talker

Posts: 3
Location: Germany
Trades: 0
Quote:
Originally Posted by JeremyMiller View Post
Call me crazy, but the page you link to doesn't have a single "onclick" in the source.
Not calling you crazy; I couldn't solve the problem so I switched to the PHP only version which I had used for browsers with JavaScript disabled.

I just reverted it to the JS-Version. Could you take a look again?
Thanks for responding.
sigdrifa is offline
Reply With Quote
View Public Profile
 
Old 02-27-2010, 05:57 AM Re: Toggle visibility script stopped working
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Checking in 5 minutes.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 02-27-2010, 06:21 AM Re: Toggle visibility script stopped working
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
I was able to isolate it down to if I remove:
HTML Code:
<script type="text/javascript" src="http://www.kulturinsel-celle.de/wp-content/themes/kulturinsel-celle.de/data/lightbox/js/prototype.js"></script>
After a closer check, "toggle" is defined within prototype, so you're having a naming conflict. Try naming your toggle function something highly unique (e.g. MyAwesomeToggleFunction) and then see if that resolves the issue.

Also, there's no point in this line of code:

if (element.style.display == 'block');

It's basically just telling the browser to waste time.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 02-27-2010, 07:14 AM Re: Toggle visibility script stopped working
sigdrifa's Avatar
Junior Talker

Posts: 3
Location: Germany
Trades: 0
Thank you, thank you, thank you so much, you saved my life

Never would have thought of that myself. I used the lightbox script on several Websites, but never once took a closer look at the code since I don't understand it anyway - my JavaScript knowledge is very limited.

As for that single line, no idea how that got in. I've been copying and pasting a few times, and it must have slipped my attention when I got frustrated.

Thanks again
__________________
Sigdrifa
sigdrifa is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Toggle visibility script stopped working
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17657 seconds with 12 queries