views:

8041

answers:

12

I am using jQuery's toggle() to show/hide table rows. It works fine in FireFox but does not work in IE 8.
show()/hide() work fine though.
slideToggle() does not work in IE either - it shows for a split second then disappears again. Works fine in FireFox.

My HTML looks similar to this

<a id="readOnlyRowsToggle">Click</a>  
<table>  
  <tr><td>row</td></tr>  
  <tr><td>row</td></tr>  
  <tr class="readOnlyRow"><td>row</td></tr>  
  <tr class="readOnlyRow"><td>row</td></tr>  
  <tr class="readOnlyRow"><td>row</td></tr>  
</table>

JavaScript

$(document).ready(function() {  
    $(".readOnlyRow").hide();  
    $("#readOnlyRowsToggle").click(function() {  
        $(".readOnlyRow").toggle();  
    });  
});
A: 

http://groups.google.com/group/jquery-en/browse_thread/thread/65f58538930f00c2

jAST
-1: adding an event didn't help
Robert MacLean
A: 

because you are having them click an <a>, you need the function to return false.

contagious
-1: not that at all. I can put anything I want in the click and it works, just not the toggle
Robert MacLean
+5  A: 

Remove the period from your <tr class=".readOnlyRow"><td>row</td></tr>. The syntax for jQuery class selecting is to prepend it with a period, but you don't need it in your HTML code.

Cory Larson
Well spotted, but not the cause unfortunately. That was caused by my retyping into the question. I have fixed it up the question, but the problem still remains.
Robert MacLean
Have you tried simply defining some CSS for your .readOnlyRow class with display:none? You could then toggle the CSS class (toggleClass) instead of having jQuery figure out the visibility toggling.
Cory Larson
A: 

I've noticed this as well. Likely you'll have to toggle a class on each td instead. Haven't tried this solution yet, so let me know!

For others - this is specific to IE8, not 6 or 7.

EDIT

Clearly you didn't try this as evidenced by the -1, as I just did with fine results (in my situation).

CSS

tr.hideme td { display: none }

JS

$("#view-advanced").click(function() {
    $("tr.hideme td").toggle();
    return false;
});
ScottE
Robert MacLean
In my first test I actually removed the class from the tr and put it on the TD, which is where I got the weird results.I have now tried changing it with the way you suggested (excluding CSS) in your edit (i.e. changing the selector) and that selector does not work in either FF or IE8. I also tried the following combinations of selectors and those didn't work either:tr.readOnlyRow > td.readOnlyRow > td.readOnlyRow tdI then tried changing the hide function to the CSS and ended up with the same weird issue as before.
Robert MacLean
Not sure it doesn't work for you - works fine for me in IE6 - 8, FF3, Safari.
ScottE
Which version of jQuery are you each using?
Cory Larson
most recent - 1.3.2
ScottE
+25  A: 

I've experienced this same error on tr's in tables. I did some investigation using IE8's script debugging tool.

First I tried using toggle:

$(classname).toggle();

This works in FF but not IE8.

I then tried this:

if($(classname).is(":visible"))//IE8 always evaluates to true.
     $(classname).hide();
else
     $(classname).show();

When I debugged this code, jquery always thought it was visible. So it would close it but then it would never open it back.

I then changed it to this:

var elem = $(classname)[0];
if(elem.style.display == 'none')
     $(classname).show();
else
{
     $(classname).hide();      
}

That worked fine. jQuery's got a bug in it or maybe my html's a little screwy. Either way, this fixed my issue.

Brian Bolton
Love it. I found this bug also and this suggestion fixed my problem within seconds. Many Thx.
Aaron
Thanks dude. Another episode of 'StackOverflow saved my job!'
ip
elem.style.display should be elem.css('display')
Martin
It seems like this line: var elem = $(classname)[0];limits the function to only toggling the first element [0] with that classname. What if you have a few page elements that you want toggled at the same time? How can you get all elements to toggle correctly?
Jen
@Jen: $(classname)[0] does not only toggle the first element, it just checks if the first of all the selected elements is visible or hidden. Based on that element all selected elements (including the first one) will be shown or hidden.
Jules
@Martin - that would only work if line 1 was `var elem = $(classname)`. However, the `[0]` retrieves the DOM element to be used rather than the jQuery object. DOM elements don't have a `css()` function, thus your replacement would not work.
Matt Huggins
A: 
$(document).ready(function() {  
    $(".readOnlyRow").hide();  
    $("#readOnlyRowsToggle").click(function() {  
        $(".readOnlyRow").toggle($('.readOnlyRow').css('display') == 'none');  
    });  
});

There is a jQuery bug that IE8 causes everything to evaluate to true. Try above

+2  A: 

Just encountered this myself -- the easiest solution I've found is to check for:

:not(:hidden)

instead of

:visible

then act accordingly . .

kaneuniversal
did not work for me
Nir
Made the difference in IE8 here. Thanks!
Alec
+1  A: 

I found that while this does not work in IE8

$('.tableRowToToggle').toggle();

but this does work:

$('.tableRowToToggle').each(function(){this.toggle()});

Got the idea from the link jAST posted here before

+1  A: 

I fixed this problem by hiding children of the TR element.

toggleTr($(".toggletr"));

function toggleTr(el) {
    $(el).children('td').each(function() {
        $(this).toggle();
    });
}

This seems to be ok for FF3.5/FF3/IE8/IE7/IE6/Chrome/Safari.

J. Visser
+4  A: 

Upgrading to jQuery 1.4 fixes this, whilst I'm at it though, the only "fix" on this page which worked for me was Dough boy's answer:

$(document).ready(function() {  
  $(".readOnlyRow").hide();  
  $("#readOnlyRowsToggle").click(function() {  
    $(".readOnlyRow").toggle($('.readOnlyRow').css('display') == 'none');  
  });  
});
Robin Duckett
I've read the docs on jQuery's .toggle()...I still don't get how the toggle code above works. Could you/someone please explain it?
runrig
Nevermind, I now see the docs for calling with a boolean: .toggle( showOrHide )
runrig
A: 

Looks like this has been fixed in JQuery 1.4.

Maciej
A: 

blubb

i had the same issue, but i found a nice work around to make toggle/slidetoggle work in all browsers.

<a id="myButton">Click</a>
<div id="myDiv" display="none">lorem ipsum .... text ....</div>  

and now the JS:

    jQuery("#myButton").click(function () {
  var currentDisplay = jQuery("#myDiv").css("display"); // save the current Display value
  jQuery(#myDiv).slideToggle("fast", function () { // lets do a nice slideToggle
    // this code will run AFTER the sildeToggle is done
    // so if slideToggle worked fine (in FF, chrome, ...) this will have no evect, since the value is already block or none
    // but in case IE 8 runs this script, it will set the css display to the currect value
    if (currentDisplay == "none") {
      jQuery(#myDiv).css('display', 'block');
    }else {
      jQuery(#myDiv).css('display', 'none');
        }
    });
    return false;
});

the result is, that slideToggle works fine in all Browsers. Exept in IE8, it will look a weird (fucked up Slide), but it will still work.

greetings <°(((-<

Zauberfisch