Forum Moderators: open

Message Too Old, No Replies

Javascript and CSS display

Mozilla likes "table-row" for tr and IE likes "block"

         

bubblebug

9:04 am on Jun 29, 2005 (gmt 0)

10+ Year Member



Hi

I am trying to do a small questionnaire with check boxes opening and closing questions according to what the user checks.

I have no problems with the opening closing stuff I have almost finished the job but am stumped on this.

I have a tr tag in a table of questions and when a user check a boxs the table row appears and disapears by using a javascript function that changes the display property of the row.

In explorer I use display: block and it works fine.

In Mozilla it does not like it, and draws the cells out of sync. Unless you use display: table-row.

This works fine with mozilla but IE will not accept table-row as a display property in this instance.

Is there a happy medium that I have missed?

bubblebug

10:43 am on Jun 29, 2005 (gmt 0)

10+ Year Member



I have now fixed this.

A simple browser detection line in the javascript.

It was obvious really.

if (navigator.appName == "Microsoft Internet Explorer") {

var displayType = 'block';

} else {

var displayType = 'table-row';

}

document.getElementById(blockID).style.display = displayType;

If anyone knows why not to do it that way (it is probably equally obvious) I would love to hear.

Rambo Tribble

1:05 pm on Jun 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rather than just trap for IE, you might wish to test for the actual property's existence, as with:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
td{border:4px solid;}
</style>
</head>
<body>
<table>
<tr onclick="alert(this.currentStyle? this.currentStyle.display : getComputedStyle(this,null).getPropertyValue('display'));">
<td>&nbsp;</td>
</tr>
</table>
</body>
</html>

Note: The ternary needs to be on one line; it is the forum that breaks it across two lines.

kaled

9:29 am on Jun 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should be able to adapt the following to your needs.

// Show/hide DIV
// Tested on IE, Opera, Netscape/Mozilla

function toggleDiv(DivName)
{ with (document.getElementById(DivName).style) {
if (display == "none")
display = ""
else display = "none";
}}

Despite the name, it should work any element, not just divs. You should note that the trick is to set style.display="" to display it.

Kaled.

bubblebug

10:13 am on Jun 30, 2005 (gmt 0)

10+ Year Member



Rambo Tribble
I could get the example you showed me to work, but I could not get my head around how to convert the code so the display value would translate to a variable in the function I had written.

Thanks anyway I have made a mental note of the possibility for future reference.

Kaled
The trick in my case was indeed to use display='' instead of display='block' or display='table-row'

What stumped me was I had used a css class to make the table row vanish when the page was drawn initially and so it did not work straight away. When I altered the tr tag to have style="display: none" instead of class="rowHide" it worked a treat.

Thanks again :)

bubblebug

10:18 am on Jun 30, 2005 (gmt 0)

10+ Year Member



Also, now it works in Opera, which was identifying itself as Microsoft Internet Explorer but behaving like Mozilla.