Web Analytics Made Easy -
StatCounter why can't I change the bgcolor in a layer? - CodingForum

Announcement

Collapse
No announcement yet.

why can't I change the bgcolor in a layer?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • why can't I change the bgcolor in a layer?

    I have the following simple script. I can make the layer hide & show, but I can't seem to change the bgcolor. When I throw the 1st alert(xLayer.bgcolor) I get undefined. The next time I display xLayer.bgcolor, I get "blue"

    <HTML>
    <HEAD><SCRIPT src=/javascript/special.js>

    </SCRIPT>
    <SCRIPT>
    function chg(color)
    {
    var xLayer=document.layers["mgt_layer2"];
    alert(xLayer.bgcolor); //get undefined
    //xLayer.bgcolor="blue";
    xLayer.bgcolor=color
    //xLayer.visibility="hide";
    alert(xLayer.id); //get mgt_layer2
    alert(xLayer.bgcolor); //get blue
    return true
    }
    </SCRIPT>
    </HEAD><BODY BGCOLOR='FFFFFF' TEXT='000000' ALINK=WHITE VLINK=WHITE LINK=WHITE>
    <FORM NAME=emp_form METHOD=POST>
    <TABLE CELLPADDING=0 CELLSPACING=0>
    <TR><TH><FONT FACE=ARIAL SIZE=2>Organization Code:
    <TD ALIGN=CENTER><FONT FACE=ARIAL SIZE=2>
    <INPUT TYPE="TEXT" NAME="org_code2" VALUE="" READONLY MAXLENGTH=25 SIZE=25 >
    <TR><TH><FONT FACE=ARIAL SIZE=2>Segment Code:<TH>
    <SELECT NAME=seg SIZE=4>
    <OPTION VALUE=1A2000000 ><FONT FACE=ARIAL SIZE=2>0000</FONT></OPTION>
    <OPTION VALUE=1A2300000 ><FONT FACE=ARIAL SIZE=2>Z000</FONT></OPTION>
    <OPTION VALUE=1A230A000 ><FONT FACE=ARIAL SIZE=2>A000</FONT></OPTION>
    <OPTION VALUE=1A230E000 ><FONT FACE=ARIAL SIZE=2>E000</FONT></OPTION>
    <OPTION VALUE=1A230F000 ><FONT FACE=ARIAL SIZE=2>F000</FONT></OPTION>
    <OPTION VALUE=1A230K000 ><FONT FACE=ARIAL SIZE=2>K000</FONT></OPTION>
    <OPTION VALUE=1A230N000 ><FONT FACE=ARIAL SIZE=2>N000</FONT></OPTION>
    </SELECT>
    <TH><INPUT TYPE=BUTTON value=GO>
    <TR><TD>
    Management: <input name="management" type="radio" value="yes" onclick="chg('blue')"> <strong> yes
    <input name="management" type="radio" value="no" onclick=" document.mgt_layer3.visibility='hide'"> no </strong>
    </TABLE>
    </FORM>
    <BR><BR>
    <LAYER name=mgt_layer2 bgcolor="yellow">Text inside layer 2 </LAYER><BR>
    <LAYER id=mgt_layer3>Text inside layer 3 </LAYER>
    </HTML>

  • #2
    you forgot style


    it goes.........style.background="#808080"
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

    Comment


    • #3
      I tried the following, but it still didn't work for me.

      xLayer.style.background="#808080";

      Comment


      • #4
        Layer.bgColor

        [case-sensitive]

        Also: use id for NS4 layers, it's supported. No .style object in Navigator.

        Comment


        • #5
          I changed name back to id within the layer tag. I had it this way originally, but it didn't work.

          I'm not sure how what you mean by using Layer.bgColor. How do you differentiate between the two layers mgt_layer2, mgt_layer3?

          function chg(color)
          {
          var xLayer=document.Layer["mgt_layer2"];
          xLayer.bgColor=color

          return true
          }

          Comment


          • #6
            function chg_bgColor(id, color)
            {
            if (document[id]) document[id].bgColor = color;
            }

            Might as well make it more flexible (instead of hardcoding in the layer id). All named/id'd layers can be referenced as document properties, no need to go through the .layers[] collection.

            Layer.bgColor...Layer here is a generalization, representing the Layer class of objects; in actual use, you'd replace it with an actual layer reference.

            Comment


            • #7
              Thanks again! That worked like a charm.

              Comment

              Working...
              X