views:

39

answers:

2

Hi,

i am trying to implement toggle function with a table row. Everything is going fine except in IE8. The script which i was used is given below.

<script src="jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">  
    $(document).ready(function(){
        $("#report tr:odd").addClass("odd");
        $("#report tr:not(.odd)").hide();
        $("#report tr:first-child").show();

        $("#report tr.odd").click(function(){
            $(this).next("tr").each(function (){ this.toggle()});
            $(this).find(".arrow").toggleClass("up");
        });
        //$("#report").jExpand();
    });
</script>        

somebody please help me..

Jessica

+1  A: 

You need to wrap ($()) your this (which is just a DOM element inside that loop, not a jQuery object) to have access to .toggle(), like this:

$(this).toggle()

But there's no need for the .each() loop here, this:

$(this).next("tr").each(function (){ $(this).toggle()});

can just be this:

$(this).next("tr").toggle();

And it'll operate on all of the elements found...even though there will be only one here.


Issue #2 is the fact that IE8 specifically thinks the next row is always visibile (this is a bug in the jQuery 1.3.2 implementation you're using). You have 2 options here, the quick fix is to re-write it like this:

$("#report tr.odd").click(function(){
    var show = $(this).find(".arrow").toggleClass("up").hasClass("up");
    $(this).next("tr").toggle(show);
});

You can see that working here. Or a better solution to me, upgrade to the latest jQuery (1.4.3), you can test your current code here against it, working in IE8 as well.

Nick Craver
later it was like that. butTHAT TOO FAILS.
jessi
@jessi - Do you have an example page? We can't say what will fix your issue 100% without the markup, but the problem I describe/showed how to solve above did for 100% sure throw an error.
Nick Craver
yes, i got the example from the below URL http://www.jankoatwarpspeed.com/examples/expandable-rows/
jessi
@jessi - The issue is with height detection in IE8 with jQuery 1.3.2 thinking the row is *already* visible, this was changed in later versions, you can either change your code or update jQuery...I've elaborated on both above in an updated asnwer.
Nick Craver
Thanks a lot Nick Craver, Pekka, EMMERICH for helping me on this issue.. Its working now..
jessi
@jessi - welcome :) be sure to [accept answers](http://api.jquery.com/multiple-selector/) on this and future questions :)
Nick Craver
A: 

In your line:

$(this).next("tr").each(function (){ this.toggle()});

Try changing it to:

$(this).next("tr").each(function (){ $(this).toggle()});

I've had this problem before on some browsers not being able to differentiate between this and $(this).

So you'll get:

$(document).ready(function(){
  $("#report tr:odd").addClass("odd");
  $("#report tr:not(.odd)").hide();
  $("#report tr:first-child").show();

  $("#report tr.odd").click(function(){
    $(this).next("tr").each(function (){ $(this).toggle()});
    $(this).find(".arrow").toggleClass("up");
  });
});
EMMERICH
This isn't a browser specific problem, this would *never* work.
Nick Craver
done, but its not working..
jessi
oop, you're right. I think when I was having the problem it may have been a little different.
EMMERICH
i made it $(this).next("tr").toggle(); but not working (only in IE 8), is there any solution ? please help me.
jessi
well I've copied your code, applied the fix we talked about and it works.
EMMERICH
in Internet Explorer 8 ?
jessi
yes, it works in IE8 too.i've put my code in my answer
EMMERICH