views:

116

answers:

4

I believe something on this code chunk is not properly coded making it incompatible on XHTML 1.0 Transitional.

The code snippet collapses div and then on click they will expand.

Can somebody see what could be wrong?

<script type="text/javascript">

function toggle(div){
el = document.getElementById(div);
    if (el.style.display == 'none')
    {
        el.style.display = '';
    } else {
        el.style.display = 'none';
    }
}

</script>

I have this before the <BODY> tag. If I remove the DOCTYPE another menu I have stops working but then the rows expand by default and aren't collapsed.

A: 

Instead of el.style.display = ''; try el.style.display = 'block';.

Topher Fangio
that would be worse. `display=''` nicely reverts to default, which may need to be `table-row`, `inline`, etc.
porneL
@porneL - Does it revert to the default on all browsers?
Topher Fangio
Yes. Perfectly in all cases. `.style` is just convenient interface to HTML `style` attribute. It's not a actual computed style of the element, so there's no harm in removing _the override_ (just like there is no harm in removing `style` attribute from HTML).
porneL
A: 

You shouldn't set the style.display = '', but rather set it to 'block', 'inline' or 'table-row'.

Shawn Steward
or use table-row
darren
@darren - Thanks, updated my answer.
Shawn Steward
+2  A: 
  1. You should check if passed argument is valid (e.g. not undefined, empty string, etc.).
  2. You should check if document.getElementById(div) found the element (e.g. even valid name will not work while document is loading and <script> is earlier in the source than element you're referring to).
  3. You're creating global variable el (put var in front of assignment).
  4. W3C recommends Transitional DTD should be avoided whenever possible, and today it is possible not to use <font> and bgcolor. Use XHTML Strict, unless you don't know what a MIME type is, then use HTML5.
porneL
A: 

It ended up being a problem with a simple display css attribute on the div. There was a markup error.

Codex73