Forum Moderators: open

Message Too Old, No Replies

toggle works in FF, not IE

beyond my capabilities!

         

sldesigns

7:06 pm on Nov 21, 2005 (gmt 0)

10+ Year Member



I'm not familiar with JavaScript, but I've got toggling working in FF, but doesn't work in IE (I'm on a Mac here if that makes any different). The source I referenced works in IE. . . and I don't see a difference myself. Your expert eyes will be greatly appreciated!
Here's the javascript in the head:
<script language="Javascript">
function toggle( targetId ) {
   if ( document.getElementById ) {
    target = document.getElementById( targetId );
    if ( target.style.display == "none" ) {
     target.style.display = "";
    } else {
     target.style.display = "none";
    }
   }
}
</script>

and the html of the toggle:

 <p><img src="img.jpg" width="57" height="43" /> <a href="javascript:toggle('railpump')">Rail Pump <span class="readmore">&darr; show/hide </span></a></p>
<div id="railpump" style="display: none;">
<p class="subhead">Text</p>
<p><img src="resources/pumpbench.jpg" width="300" height="225" /></p>
<p>Blah blah explanation text</p>
<p><a href="#top">&raquo; back to top</a></p>
<p>&nbsp;</p>
</div>

THANK YOU!

Bernard Marx

2:02 am on Nov 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This works in IE Win. I can't see any specific reason for it not working on a mac, but you could try assigning "block" instead of assigning an empty string and relying on the default...

target.style.display = "";
// becomes
target.style.display = "block";

kaled

11:28 am on Nov 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If possible, set IE to report scripting errors. That may yield a clue.

Kaled.

sldesigns

1:01 pm on Nov 22, 2005 (gmt 0)

10+ Year Member



Thanks for the tip on error checking. I get an error:

line 10, invalid character

which is this line:
   if ( document.getElementById ) {

I realize I'm going to have to learn some of this (in my free time!) but over my head here. Is this just IE mac -- and I won't worry about it then --- or does it have potential to fall apart in other browsers?

<script language="Javascript">
function toggle( targetId ) {
   if ( document.getElementById ) {
    target = document.getElementById( targetId );
    if ( target.style.display == "none" ) {
     target.style.display = "";
    } else {
     target.style.display = "none";
    }
   }
}
</script>

kaled

2:45 pm on Nov 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this

function toggleDisplay(id)
{ var target = document.getElementById(id);
if (target) with (target.style) {
if (display!= "none") display = "none"; else display = ""
}}

Kaled.

sldesigns

2:22 pm on Nov 23, 2005 (gmt 0)

10+ Year Member



I admit the possibility of user error -- replaced the previous code with this, but it didn't work. Broke even in ff. error "expected element"

Well, the client is testing on a pc and loves it, ff works on the mac; as long as ie7 doesn't ruin everything, the old code will work.

If the curious without a mac have other suggestions, I'm still willing to try and let you know how it goes.

kaled

6:56 pm on Nov 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just did a quick cut and paste test on the code above and it worked fine on Firefox 1.04 (under XP).

Kaled.