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 still doesn't work in Firefox
Old 05-09-2008, 11:57 PM Toggle Visibility still doesn't work in Firefox
Novice Talker

Posts: 7
Trades: 0
Hi,
I've searched and searched, and tested more than once.
I'm trying to get the following javascript to work in firefox.(works great in IE)
If I fix it replacing w/ display/block instead of visibility/hidden it works in firefox but not in ie....

<script language="JavaScript">
function toggleVisibility(me){
if (me.style.visibility=="visible"){
me.style.visibility="hidden";
}
else {
me.style.visibility="visible";
}
}
</script>

Markup :
<a onclick="toggleVisibility(mydiv)">Click to make visible</a>
<div id="mydiv" name="mydiv">Show this DIV</div>
firemail is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-10-2008, 01:50 AM Re: Toggle Visibility still doesn't work in Firefox
Average Talker

Posts: 18
Name: TK
Trades: 0
Trying this in both IE7 and Firefox has the same behavior for me: first click has no effect the second and subsequent clicks work. This is because when the function is called the first time, the div's visibility attribute is undefined though the browsers default to making it visible. That makes the if statement fall through to the else branch, where the div then gets assigned an explicit style of "visibility: visible;" which of course has no effect.

You can account for this by switching the if statement to test for "hidden" first.

HTML Code:
<html>
<head>
 <title>nothing</title>
 <script language="JavaScript" type="text/javascript">
 <!--
function toggleVisibility(me){
  if (me.style.visibility=="hidden"){
     me.style.visibility="visible";
  }
  else {
     me.style.visibility="hidden";
  }
}
// -->
 </script>
</head>
<body>
 <a onclick="toggleVisibility(mydiv)">Click to make visible</a>
 <div id="mydiv" name="mydiv">Show this DIV</div>
</body>
</html>

Last edited by zxcvbnm60; 05-10-2008 at 02:19 AM..
zxcvbnm60 is offline
Reply With Quote
View Public Profile
 
Old 05-10-2008, 11:03 AM Re: Toggle Visibility still doesn't work in Firefox
Novice Talker

Posts: 7
Trades: 0
Ok, this script works fine in IE(all). It doesn't work on Firefox.
The inherit value of the div I'm try to make "visible" is "hidden".

You can see it in developpment @ http://firemail.ca/phpsearch/
it's the "login link" @ top right.

It works just great w/ IE and other browsers, but not Firefox.

Could I replace the javascipt w/ a PHP function, the site is built up in PHP.

Why am I having the same problems w/ Firefox, that I had w/ Netscape ten years ago?
firemail is offline
Reply With Quote
View Public Profile
 
Old 05-10-2008, 01:48 PM Re: Toggle Visibility still doesn't work in Firefox
Average Talker

Posts: 18
Name: TK
Trades: 0
Ah, I knew there was a reason I've never trusted passing around elements by name as parameters. There's no shame in using getElementById and passing the id of the div you want to toggle.

Minor changes:

Put 'mydiv' in single quotes in the onclick handler.
Code:
<a href="#" onClick="toggleVisibility('mydiv');">
Change the function declaration line and set me to be the element returned by getElementById:
Code:
    function toggleVisibility(sId){
                me=document.getElementById(sId);
That should work for most browsers.
zxcvbnm60 is offline
Reply With Quote
View Public Profile
 
Old 05-10-2008, 02:34 PM Re: Toggle Visibility still doesn't work in Firefox
Novice Talker

Posts: 7
Trades: 0
Great,
I've tried dozens of variations all failed, thanks.

-----Disregard following lines -----
New problem though,
first click does nothing! Second click only, tried reversing hidden w/ visibile and VS, not working..
---------------------------------
IT WORKS GREAT!
Alex

Last edited by firemail; 05-10-2008 at 06:04 PM.. Reason: Tried the wrong way twice
firemail is offline
Reply With Quote
View Public Profile
 
Old 05-11-2008, 03:00 AM Re: Toggle Visibility still doesn't work in Firefox
Average Talker

Posts: 18
Name: TK
Trades: 0
So does it work or not? Final code:
Code:
 
<html>
<head>
 <title>nothing</title>
 <script language="JavaScript" type="text/javascript">
 <!--
function toggleVisibility(sId){
  var me=document.getElementById(sId);
  if (me.style.visibility=="hidden"){
     me.style.visibility="visible";
  }
  else {
     me.style.visibility="hidden";
  }
}
// -->
 </script>
</head>
<body>
 <a onclick="toggleVisibility('mydiv')">Click to make visible</a>
 <div id="mydiv" name="mydiv">Show this DIV</div>
</body>
</html>

Last edited by zxcvbnm60; 05-11-2008 at 03:04 AM..
zxcvbnm60 is offline
Reply With Quote
View Public Profile
 
Old 05-11-2008, 09:36 PM Re: Toggle Visibility still doesn't work in Firefox
Novice Talker

Posts: 7
Trades: 0
No no it works great!

I had inverted the values, it's ok now.

Thanks
firemail is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Toggle Visibility still doesn't work in Firefox
 

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.26633 seconds with 12 queries