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
    Jun 2007
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts

    background color

    Hi,

    I have the following javascript codes.
    I have two textboxes with id "pano" and "pano2"
    My function creates numbers randomly and that if random number==1 let the background of textbox2 be pink and if random number==2
    textbox1 be red. The code works fine for document.bgColor but it does not work for textboxes
    how can I modify the code so that I can change the background color of any textboxes ?

    thanks..





    <script>
    aaa=setInterval("letsgo()",500);
    function letsgo()
    {
    rno=Math.floor(Math.random()*3)+1;
    document.getElementById("counter").value=rno;
    if(rno==3)
    {
    document.bgColor="green";
    }
    if(rno==2)
    {
    var pano=document.getElementById("pano");
    document.pano.bgColor="red";

    }
    if(rno==1)
    {
    var pano2=document.getElementById("pano2");
    document.pano2.bgColor="pink";

    }
    }
    </script>
    <input type="text" id="counter" style="position:absolute;top:10px;left:50px;width:60px;height:20px;">
    <input type="text" id="pano" style="position:absolute;top:100px;left:100px;width:200px;height:200px;">
    <input type="text" id="pano2" style="position:absolute;top:100px;left:400px;width:200px;height:200px;">

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    bgColor is a long obsolete property, anyway.

    You should use something like
    Code:
        document.getElementById("pano2").style.backgroundColor = "pink";
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks a ton OLDPEDANT

    you did a great jop..and saved the day..

    it works fine..

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Hi Old pedant

    one more question.. what if I have checkbox instead of a text box.? How can I modify my code so if random number==1 let checkbox1 be checked and that if random number ==2 checkbox2 be checked.?

    thanks in advanced

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Will the checkboxes have the same name? Or different names?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New Coder
    Join Date
    Jun 2007
    Posts
    38
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Will the checkboxes have the same name? Or different names?
    different names

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Code:
    var which = Math.floor( Math.random() * 2 );
    document.forms[0].firstCheckBoxName.checked = (which == 0);
    document.forms[0].otherCheckBoxName.checked = (which == 1);
    Just one way of doing it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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