How to change cell background?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith Smith

    #1

    How to change cell background?

    How can I change the background IMAGE (not just color) of a CELL in a table?

    I know that I can do this using CSS, but I really need to be able to do it
    using JavaScript. Anyone know how?

    Must be able to change the image for each cell - not the whole table.

    Please help.


  • Mick White

    #2
    Re: How to change cell background?

    Keith Smith wrote:
    [color=blue]
    > How can I change the background IMAGE (not just color) of a CELL in a table?
    >
    > I know that I can do this using CSS, but I really need to be able to do it
    > using JavaScript. Anyone know how?
    >
    > Must be able to change the image for each cell - not the whole table.
    >
    > Please help.
    >
    >[/color]

    document.getEle mentById('foo') .style.backgrou ndImage = "url(bar.gi f)" ;

    Mick

    Comment

    • RobB

      #3
      Re: How to change cell background?

      Keith Smith wrote:[color=blue]
      > How can I change the background IMAGE (not just color) of a CELL in a[/color]
      table?[color=blue]
      >
      > I know that I can do this using CSS, but I really need to be able to[/color]
      do it[color=blue]
      > using JavaScript. Anyone know how?
      >
      > Must be able to change the image for each cell - not the whole table.
      >
      > Please help.[/color]

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd">
      <html>
      <head>
      <style type="text/css">


      #foo td {
      width: 200px;
      height: 200px;
      }
      ..test {
      background: url(http://frostandkeading.com/K07_200-x-200.jpg)
      no-repeat;
      }

      </style>
      <script type="text/javascript">

      function setCellBG(tdref , imgsrc)
      {
      if (null != tdref && tdref.style)
      tdref.style.bac kgroundImage = 'url(' + imgsrc + ')';
      }

      window.onload = function()
      {
      setTimeout(
      function()
      {
      var src = 'http://frostandkeading .com/K22_200-x-200.jpg';
      var td = document.getEle mentById('foo') .rows[1].cells[1];
      setCellBG(td, src);
      }, 3000);
      }

      </script>
      </head>
      <body>
      <table>
      <tbody id="foo">
      <tr>
      <td></td>
      <td></td>
      <td></td>
      </tr><tr>
      <td></td>
      <td class="test"></td>
      <td></td>
      </tr>
      </tbody>
      </table>
      </body>
      </html>

      Comment

      • azcn2503@gmail.com

        #4
        Re: How to change cell background?

        Here is a handy function that lets multiple browsers perform this:

        Put this in the <HEAD>:

        <script type="text/javascript"><!--
        function grab(o){return
        document.all?do cument.all[o]:document.getEl ementById?docum ent.getElementB yId(o):"";}
        //--></script>

        Then when you want to grab an object by its ID attribute:

        <script type="text/javascript"><!--
        grab("tablecell ").style.backgr oundImage="url( images/image1.jpg)";
        //--></script>

        Comment

        • Keith Smith

          #5
          Re: How to change cell background?

          > <script type="text/javascript"><!--[color=blue]
          > function grab(o){return
          > document.all?do cument.all[o]:document.getEl ementById?docum ent.getElementB yId(o):"";}
          > //--></script>
          >
          > Then when you want to grab an object by its ID attribute:
          >
          > <script type="text/javascript"><!--
          > grab("tablecell ").style.backgr oundImage="url( images/image1.jpg)";
          > //--></script>[/color]

          It doesn't seem to work for me. Would someone mind giving it a try and let
          me know if it works for you?


          Comment

          • RobG

            #6
            Re: How to change cell background?

            Keith Smith wrote:[color=blue][color=green]
            >><script type="text/javascript"><!--
            >>function grab(o){return
            >>document.all? document.all[o]:document.getEl ementById?docum ent.getElementB yId(o):"";}[/color][/color]

            A more efficient test is to put document.getEle mentById first, as it
            is supported by a wider range of browsers than document.all.
            Therefore most browsers will not go to the second test. Rearranging
            gives:

            return document.getEle mentById? document.getEle mentById(o) :
            document.all? document.all[o] : "";
            [color=blue][color=green]
            >>//--></script>
            >>
            >>Then when you want to grab an object by its ID attribute:
            >>
            >><script type="text/javascript"><!--
            >>grab("tablece ll").style.back groundImage="ur l(images/image1.jpg)";[/color][/color]

            When using the style object, you should test to see if it's supported
            first, otherwise the user may be presented with an error message that
            they have no idea how to remedy.
            [color=blue][color=green]
            >>//--></script>[/color]
            >
            >
            > It doesn't seem to work for me. Would someone mind giving it a try and let
            > me know if it works for you?
            >[/color]

            Here is a full page below, there are many different ways to write
            this, below is just an example. Over to you.



            <html><head><ti tle>play</title></head>
            <body>

            <script type="text/javascript">

            // Given an element id,
            // return a reference to the element
            function grab(x){
            return document.getEle mentById? document.getEle mentById(x) :
            document.all? document.all[x] : "";
            }

            // Given an element id and image,
            // change the background of the element to the image
            function changeBGimg(id, image){
            var x = grab(id);
            if (x.style){
            x.style.backgro undImage = 'url(' + image + ')';
            }

            }

            </script>
            <table border="1">
            <tr>
            <td onclick="
            changeBGimg('ce llA','a.jpg');
            ">Click to change background</td>
            <td id="cellA"><di v style="width:20 0px; height: 150px;"></div></td>
            </tr>
            </table>
            </body</html>



            --
            Rob

            Comment

            • Grant Wagner

              #7
              Re: How to change cell background?

              <azcn2503@gmail .com> wrote in message
              news:1114336166 .384669.145100@ g14g2000cwa.goo glegroups.com.. .[color=blue]
              > Here is a handy function that lets multiple browsers perform this:
              >
              > Put this in the <HEAD>:
              >
              > <script type="text/javascript"><!--
              > function grab(o){return
              > document.all?do cument.all[o]:document.getEl ementById?docum ent.getElementB yId(o):"";}
              > //--></script>[/color]

              Instead of feature testing everytime you execute grab(), one can define
              grab once at the beginning of the page and then reasonably assume that
              the feature support will not change in that user agent for the lifetime
              of the script:

              <script type="text/javascript">
              var grab;
              if (document.getEl ementById)
              {
              grab = function(o)
              {
              return document.getEle mentById(o);
              }
              }
              else if (document.all)
              {
              grab = function(o)
              {
              return document.all[o];
              }
              }
              else
              {
              grab = function()
              {
              return { style:{} };
              }
              }
              </script>
              [color=blue]
              > Then when you want to grab an object by its ID attribute:
              >
              > <script type="text/javascript"><!--
              > grab("tablecell ").style.backgr oundImage="url( images/image1.jpg)";
              > //--></script>[/color]

              Since your code makes the assumption that grab() is returning an object
              that contains a property named -style-, but in some cases your version
              of grab() returns an empty string, the line above may generate an error
              on user agents which support neither document.getEle mentById nor
              document.all.

              In cases where the user agent supports neither document.getEle mentById
              nor document.all, my solution returns an object with a -style- property
              which can other arbitrary properties assigned to it.

              Note that I'm not 100% convinced this is an adequate solution, but since
              we tend to assume that the Node returned by document.getEle mentById and
              document.all supports a -style- property, it seems reasonable to return
              an object that has one even when we don't obtain a valid Node.

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


              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: How to change cell background?

                Keith Smith wrote:
                [color=blue][color=green]
                >> <script type="text/javascript"><!--
                >> function grab(o){return
                >>[/color][/color]
                document.all?do cument.all[o]:document.getEl ementById?docum ent.getElementB yId(o):"";}[color=blue][color=green]
                >> //--></script>[/color][/color]

                Omit the Comment Declaration Open delimiter (`<!--'), the JS single-line
                comment (`//') and the Comment Declaration Close (`-->') delimiter. They
                serve no reasonable purpose here.
                [color=blue][color=green]
                >> Then when you want to grab an object by its ID attribute:
                >>
                >> <script type="text/javascript"><!--
                >> grab("tablecell ").style.backgr oundImage="url( images/image1.jpg)";
                >> //--></script>[/color]
                >
                > It doesn't seem to work for me. Would someone mind giving it a try and
                > let me know if it works for you?[/color]

                WFM, however I would rather use

                setStylePropert y("id", "tablecell" , 0,
                "backgroundImag e", "url(images/image1.jpg)");

                from <http://pointedears.de/scripts/dhtml.js> instead.


                PointedEars

                Comment

                Working...