views:

43

answers:

2

I have the following javascript

function hide(id)
{
    var ele = document.getElementById(id);
    if ((ele.style.display == 'none') || (ele.style.display == '')) {
    try{
     ele.style.display = 'table-row';
     }
     catch (e)
     {
     ele.style.display='block';
     }}
    else {ele.style.display = 'none';}
}

which works in ie7, chrome, ff, but fails in ie8

I have to get it to work in ie8 even if it fails in chrome or ff.

I believe the issue is 'ele.style.display = 'table-row'

any ideas? thanks in advance

A: 

Why don't you use:

function toggle(id) {
  var el = document.getElementById(id);
  el.style.display = (el.style.display == "none" || el.style.display == "") ? "block" : "none";
}
Andy Robinson
Because blindly setting something to "display: block" isn't a good idea. What if it should be "display: inline"? Or "display: table-cell"?
Pointy
A: 

Not sure what you are experiencing as a failure in IE8, but you might change in your if this:

ele.style.display == ''

to this:

ele.style.display == undefined
Scott