Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 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
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    you forgot style


    it goes.........style.background="#808080"

  • #3
    New Coder
    Join Date
    Jul 2002
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried the following, but it still didn't work for me.

    xLayer.style.background="#808080";

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Layer.bgColor

    [case-sensitive]

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

  • #5
    New Coder
    Join Date
    Jul 2002
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    }

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    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.

  • #7
    New Coder
    Join Date
    Jul 2002
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thanks again! That worked like a charm.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •