View Full Version : hide/show div problem

11-16-2012, 02:04 PM
I made a div hide/show script for a friend, but he complains it's not working in Firefox & Chrome. It works fine in IE though..

What do I need to alter to make this work?

<script type=text/javascript>
function toggleT(_w,_h) {
if (document.all) { // is IE
if (_h=='s') eval("document.all."+_w+".style.visibility='visible';");
if (_h=='h') eval("document.all."+_w+".style.visibility='hidden';");
} else { // is NS?
if (_h=='s') eval("document.layers['"+_w+"'].visibility='show';");
if (_h=='h') eval("document.layers['"+_w+"'].visibility='hide';");

<span id='divt2' style='visibility:visible;position:relative;top:0;left:0'><p>
Here is some text
</span></p><br><input type='submit' value='bestel' onClick=toggleT('divt1','s');toggleT('divt2','h')></span>
<span id="divt1" style="visibility:hidden;position:absolute;top:10;left:10">
<form action='klantenorder.php' method='post'>
<input type="hidden" name="couponcode" value="coup01">
<input type="hidden" name="emailklant" value="spellenkennis@live.nl">
<input type="submit" value="bestelling plaatsen">


Philip M
11-16-2012, 03:16 PM
document.all and document.layers are long obsolete (as is Netscape). Use document.getElementById() instead - supported by all modern browsers.
IE still supports document.all for historic reasons.

eval is also regarded as poor practice and is completely unnecessary.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

Logic Ali
11-16-2012, 03:58 PM
Here's a nice idiot-proof replacement function:

function toggleT( id, action )
var show = { h : 'hidden', s : 'visible' }[ typeof action == 'string' ? action.toLowerCase() : '' ] || alert( 'Second parameter must be "h" or "s" only' ),
elem = document.getElementById( id ) || alert( 'No rendered element with ID: "' + id + '"' );

if( elem )
elem.style.visibility = show;

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum