hiding table rows with javascript and css

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Fish

    #1

    hiding table rows with javascript and css

    Hi,

    I am trying to show/hide a group of table rows using javascript and CSS. if
    I group them in a <tbody> element I can do:

    my_tbody_elemen t.style.display ="none";

    to hide them and

    my_tbody_elemen t.style.display ="block";

    to show them. This works in IE but doesn't work in css-compliant browsers,
    because these use "table-row-group" instead (which IE doesn't support). I
    have tried setting the style to "" or "default" but this doesn't work
    either.

    is there a simple cross-browser solution to this, or do I have to detect
    which browser is in use?

    TIA

    Andy


  • Martin Honnen

    #2
    Re: hiding table rows with javascript and css



    Andy Fish wrote:

    [color=blue]
    > I am trying to show/hide a group of table rows using javascript and CSS. if
    > I group them in a <tbody> element I can do:
    >
    > my_tbody_elemen t.style.display ="none";
    >
    > to hide them and
    >
    > my_tbody_elemen t.style.display ="block";
    >
    > to show them.[/color]

    It should suffice to set
    my_tbody_elemen t.style.display = "";
    if all display to none settings are done with script as you have above.
    Only if you had style rules in a style sheet e.g.
    tbody { display: none; }
    the script setting the inline style
    style.display = "";
    could not show the element as an empty inline style obviously doesn't
    override a style rule.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Kiran Makam

      #3
      Re: hiding table rows with javascript and css

      > > my_tbody_elemen t.style.display ="block";

      Its working in IE because IE does not care about CSS2.
      In the above code display value should be set as 'table-row' for CSS2
      compliant browsers and 'block' for IE.

      See fully working function below:

      function toggle(tbody){
      var displayVal = (document.all)? "block" : "table-row";
      var obj = document.getEle mentById(tbody) ;
      obj.style.displ ay = (obj.style.disp lay != "none")? "none" : displayVal
      ;
      }

      Tested in NS7 and IE6.

      - Kiran Makam

      Comment

      • Richard Cornford

        #4
        Re: hiding table rows with javascript and css

        Kiran Makam wrote:[color=blue][color=green][color=darkred]
        >> > my_tbody_elemen t.style.display ="block";[/color][/color][/color]
        <snip>[color=blue]
        > function toggle(tbody){
        > var displayVal = (document.all)? "block" : "table-row";[/color]
        <snip>[color=blue]
        > Tested in NS7 and IE6.[/color]

        But testing in Opera, Safari, Konqueror or IceBrowser would have
        revealed how limited this approach is. The assumption that a browser
        implementing document.all is IE has not been valid for many years now.

        I would go along with Martin Honnen in this an use a technique that
        works cross-browser.

        Richard.


        Comment

        • Andy Fish

          #5
          Re: hiding table rows with javascript and css


          "Richard Cornford" <Richard@litote s.demon.co.uk> wrote in message
          news:csh79i$902 $1$830fa79d@new s.demon.co.uk.. .[color=blue]
          > Kiran Makam wrote:[color=green][color=darkred]
          >>> > my_tbody_elemen t.style.display ="block";[/color][/color]
          > <snip>[color=green]
          >> function toggle(tbody){
          >> var displayVal = (document.all)? "block" : "table-row";[/color]
          > <snip>[color=green]
          >> Tested in NS7 and IE6.[/color]
          >
          > But testing in Opera, Safari, Konqueror or IceBrowser would have
          > revealed how limited this approach is. The assumption that a browser
          > implementing document.all is IE has not been valid for many years now.
          >
          > I would go along with Martin Honnen in this an use a technique that
          > works cross-browser.
          >[/color]

          Well I guess there is no such thing as a guaranteed cross-browser solution
          when you're dealing with something like IE that is not standards-compliant.

          After some more hunting, I found a solution which I'm pretty sure is
          standard CSS but works in IE as well.

          <style>
          tbody.on { display:table-row-group; }
          tbody.off { display:none; }
          </style>

          <script>
          if (something) {
          my_tbody_elemen t.className = "on";
          } else {
          my_tbody_elemen t.className = "off";
          }
          </script>

          Andy

          [color=blue]
          > Richard.
          >
          >[/color]


          Comment

          • Grant Wagner

            #6
            Re: hiding table rows with javascript and css

            "Andy Fish" <ajfish@blueyon der.co.uk> wrote in message
            news:vLWGd.1261 1$GG1.1192@text .news.blueyonde r.co.uk...[color=blue]
            >
            > "Richard Cornford" <Richard@litote s.demon.co.uk> wrote in message
            > news:csh79i$902 $1$830fa79d@new s.demon.co.uk.. .[color=green]
            >> Kiran Makam wrote:[color=darkred]
            >>>> > my_tbody_elemen t.style.display ="block";[/color]
            >> <snip>[color=darkred]
            >>> function toggle(tbody){
            >>> var displayVal = (document.all)? "block" : "table-row";[/color]
            >> <snip>[color=darkred]
            >>> Tested in NS7 and IE6.[/color]
            >>
            >> But testing in Opera, Safari, Konqueror or IceBrowser would have
            >> revealed how limited this approach is. The assumption that a browser
            >> implementing document.all is IE has not been valid for many years
            >> now.
            >>
            >> I would go along with Martin Honnen in this an use a technique that
            >> works cross-browser.[/color]
            >
            > Well I guess there is no such thing as a guaranteed cross-browser
            > solution when you're dealing with something like IE that is not
            > standards-compliant.[/color]

            You are correct, there is no such thing as a guaranteed cross-browser
            solution, but for the wrong reason. The problem is that some browsers
            may not have a Style object as a property of each HTML element, or it
            may support the Style object as a property but may not dynamically
            update the screen when Style properties are changed via script.

            However, the types of problems outlined above are not what you are
            referring to. You want a simple solution that will allow you to
            change -display- property of a TBODY from "none" back to the default
            state (table-row-group), but IE does not support a -display- property
            value of "table-row-group".

            So, the solution is what Martin told you, and Richard referred you to
            that solution.

            The only possibility is that Martin's post did not appear on your news
            server. In that case, I'll quote Martin's original post:

            It should suffice to set
            my_tbody_elemen t.style.display = "";
            if all display to none settings are done with script as you have above.
            Only if you had style rules in a style sheet e.g.
            tbody { display: none; }
            the script setting the inline style
            style.display = "";
            could not show the element as an empty inline style obviously doesn't
            override a style rule.

            So the solution is to set TBODY.style.dis play = ""; (empty string). This
            will result in the HTML element displaying it's content using the
            default display mechanism for that element (block for <div>, inline for
            <span>, table-row-group for <tbody>, etc).

            --
            Grant Wagner <gwagner@agrico reunited.com>
            comp.lang.javas cript FAQ - http://jibbering.com/faq


            Comment

            • phil_gg04@treefic.com

              #7
              Re: hiding table rows with javascript and css

              I do this sort of thing:

              stylesheet:
              ..hidden {
              display: none;
              }

              script:
              my_tbody_elemen t.className="hi dden";
              my_tbody_elemen t.className="";


              I believe this is cross-browser. It is slightly more complicated if
              there are other classes present. In this case I have these functions
              to add and remove classes:

              function add_class(e,c) {
              e.className=e.c lassName+" "+c;
              }

              function remove_class(e, c) {
              cn=e.className;
              p=cn.indexOf(c) ;
              e.className=cn. substr(0,p)+cn. substr(p+c.leng th);
              }


              --Phil.

              Comment

              • DU

                #8
                Re: hiding table rows with javascript and css

                Grant Wagner wrote:[color=blue]
                > "Andy Fish" <ajfish@blueyon der.co.uk> wrote in message
                > news:vLWGd.1261 1$GG1.1192@text .news.blueyonde r.co.uk...
                >[color=green]
                >>"Richard Cornford" <Richard@litote s.demon.co.uk> wrote in message
                >>news:csh79i$9 02$1$830fa79d@n ews.demon.co.uk ...
                >>[color=darkred]
                >>>Kiran Makam wrote:
                >>>
                >>>>>> my_tbody_elemen t.style.display ="block";
                >>>
                >>><snip>
                >>>
                >>>>function toggle(tbody){
                >>>>var displayVal = (document.all)? "block" : "table-row";
                >>>
                >>><snip>
                >>>
                >>>>Tested in NS7 and IE6.
                >>>
                >>>But testing in Opera, Safari, Konqueror or IceBrowser would have
                >>>revealed how limited this approach is. The assumption that a browser
                >>>implementi ng document.all is IE has not been valid for many years
                >>>now.
                >>>
                >>>I would go along with Martin Honnen in this an use a technique that
                >>>works cross-browser.[/color]
                >>
                >>Well I guess there is no such thing as a guaranteed cross-browser
                >>solution when you're dealing with something like IE that is not
                >>standards-compliant.[/color]
                >
                >
                > You are correct, there is no such thing as a guaranteed cross-browser
                > solution, but for the wrong reason. The problem is that some browsers
                > may not have a Style object as a property of each HTML element, or it
                > may support the Style object as a property but may not dynamically
                > update the screen when Style properties are changed via script.
                >[/color]

                Also, it may support a css2 property but not entirely and there is no
                way to verify/know this with javascript. E.g.: visibility = collapse.
                Opera support documentation claims to support visibility collapse for
                rows, columns, rowgroups and column groups but it does not according to
                CSS2.1.
                [color=blue]
                > However, the types of problems outlined above are not what you are
                > referring to. You want a simple solution that will allow you to
                > change -display- property of a TBODY from "none" back to the default
                > state (table-row-group), but IE does not support a -display- property
                > value of "table-row-group".
                >
                > So, the solution is what Martin told you, and Richard referred you to
                > that solution.
                >
                > The only possibility is that Martin's post did not appear on your news
                > server. In that case, I'll quote Martin's original post:
                >
                > It should suffice to set
                > my_tbody_elemen t.style.display = "";
                > if all display to none settings are done with script as you have above.
                > Only if you had style rules in a style sheet e.g.
                > tbody { display: none; }
                > the script setting the inline style
                > style.display = "";
                > could not show the element as an empty inline style obviously doesn't
                > override a style rule.
                >
                > So the solution is to set TBODY.style.dis play = ""; (empty string). This
                > will result in the HTML element displaying it's content using the
                > default display mechanism for that element (block for <div>, inline for
                > <span>, table-row-group for <tbody>, etc).
                >[/color]

                Personally, I first use visibility = collapse for Mozilla-based browsers.

                DU
                --
                The site said to use Internet Explorer 5 or better... so I switched to
                Mozilla 1.7.5 :)

                Comment

                • Christoph Paeper

                  #9
                  Re: hiding table rows with javascript and css

                  *Andy Fish* <ajfish@blueyon der.co.uk>:[color=blue]
                  >
                  > I am trying to show/hide a group of table rows using javascript and CSS.[/color]

                  The correct CSS way would be "visibility : collapse":
                  <http://www.w3.org/TR/CSS2/tables.html#dyn amic-effects>. I've never tried
                  that and doubt it's well supported, though.
                  [color=blue]
                  > my_tbody_elemen t.style.display ="none";
                  > my_tbody_elemen t.style.display ="block";[/color]

                  IMO the best way of the JS part is a dynamically assigned class, not
                  '.style'. That's true for almost any JS-CSS interaction, a.k.a. "DHTML".
                  See <http://webdesign.criss ov.de/Scripting/modifyClass.js> for some basic
                  functions/methods for that.
                  [color=blue]
                  > This works in IE but doesn't work in css-compliant browsers, because
                  > these use "table-row-group" instead[/color]

                  tbody.show {display: block; display: table-row-group;}
                  tbody.hide {display: none;}

                  Switch¹ between those classes (if the aforementioned method indeed does
                  not work). It /should/ work and is at least better than JS browser filters.

                  --
                  "Music is essentially useless, as life is."
                  George Santayana

                  Comment

                  Working...