+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Change background color with JavaScript?

  1. #1
    gaptrast's Avatar
    gaptrast is offline x10 Sophmore gaptrast is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    117

    Change background color with JavaScript?

    Hello,

    How can I change background color with JavaScript?

    like this:

    Code:
    <div onclick="change(green)">change bakcground color</div>
    heres the head code

    Code:
    function change(){
        document.getElementById('uptd2').style.backgroundColor=();
    }
    It will not work,, please help.

  2. #2
    farscapeone's Avatar
    farscapeone is offline Community Advocate farscapeone is on a distinguished road
    Join Date
    Dec 2008
    Location
    Србија (Serbia)
    Posts
    1,166

    Re: Change background color with JavaScript?

    1. Do you actually have an element with id="uptd2"?
    2. Your function does not accept any arguments.

    You should have done it like this:

    Code:
    <div id="uptd2" onclick="change('green')">change background color</div>
    
    ....
    
    function change(color){
        document.getElementById('uptd2').style.backgroundColor = color;
    }
    * changes are in red
    Last edited by farscapeone; 09-05-2010 at 08:31 AM.

  3. #3
    gaptrast's Avatar
    gaptrast is offline x10 Sophmore gaptrast is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    117

    Re: Change background color with JavaScript?

    Well... it did only work if I had the id on the div tag....



    Here is the code who di not work for me:



    Code:
    <div onclick="change('yellow')">change background color</div>  
    
    <table>
    <tr>
    <td id="uptd2">
    
    ....  
    
    function change(color){
         document.getElementById('uptd2').style.backgroundColor = color; }
    It is the table data (td) who shall change background color, not the div...
    Last edited by gaptrast; 09-05-2010 at 11:02 AM.

  4. #4
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: Change background color with JavaScript?

    If you're doing stuff like this, I would recommend looking into jQuery

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  5. #5
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: Change background color with JavaScript?

    A 50KB download to change a background colour on a table cell? That's a bit severe, isn't it?

    Is the id "uptd2" unique on the web page? Are you using any sort of debugging aid, like Firebug or even a JavaScript error console, when developing? (And if not, why not?)
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

  6. #6
    hoj469066 is offline x10Hosting Member hoj469066 is an unknown quantity at this point
    Join Date
    Aug 2010
    Location
    Denmark
    Posts
    3

    Re: Change background color with JavaScript?

    Hi,

    This example will change the td background color when you click on the div.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="da" lang="da" >
    <head>
    <style type="text/css">
    #changeColorDiv
    {
    width: 200px;
    height: 200px;
    color: white;
    background-color: grey;
    }

    #uptd2
    {
    width: 200px;
    height: 200px;
    color: white;
    background-color: grey;
    }
    </style>

    <script type="text/javascript">
    function change(color)
    {
    document.getElementById('uptd2').style.backgroundC olor=(color);
    }
    </script>
    </head>
    <body>
    <table>
    <tr>
    <td id="uptd2"></td>
    </tr>
    </table>

    <div id="changeColorDiv" onclick="change('green');">click to change bakcground color</div>
    </body>
    </html>

  7. #7
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: Change background color with JavaScript?

    Quote Originally Posted by essellar View Post
    A 50KB download to change a background colour on a table cell? That's a bit severe, isn't it?

    Is the id "uptd2" unique on the web page? Are you using any sort of debugging aid, like Firebug or even a JavaScript error console, when developing? (And if not, why not?)
    If you link to the jQuery document hosted on google, it's usually already cached by the user, as there are a LOT of sites that link to it.

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  8. #8
    farscapeone's Avatar
    farscapeone is offline Community Advocate farscapeone is on a distinguished road
    Join Date
    Dec 2008
    Location
    Србија (Serbia)
    Posts
    1,166

    Re: Change background color with JavaScript?

    I agree with Alex Mac, jQuery rocks!!! ... and it's totally worth it. Please stop with that 50KB nonsense. Nobody uses JavaScript just to change the background color of one element. At the end the amount of JavasScript code almost always exceeds the size of jQuery library and code combined.

  9. #9
    leafypiggy's Avatar
    leafypiggy is offline Community Advocate leafypiggy is on a distinguished road
    Join Date
    Aug 2007
    Location
    Massachusetts
    Posts
    2,228

    Re: Change background color with JavaScript?

    even if it WAS 50kb, do you really have that much of a crappy down speed that it would take you long to download it? It would literally take me ~2 seconds here..
    Neil Hanlon | x10Hosting Support Representative
    Neil[at]x10hosting.com
    █ I'm always happy to help. Just ask a question in Free Hosting
    Terms of Service IRC

  10. #10
    jerome990 is offline x10Hosting Member jerome990 is an unknown quantity at this point
    Join Date
    Sep 2010
    Posts
    13

    Re: Change background color with JavaScript?

    The changes process is ;

    Code:<div onclick="change(green)">change bakcground color</div>
    heres the head code

    Code:
    function change(){document.getElementById('uptd2').style.ba ckgroundColor=();
    }It will not work,, please help.
    _______________________________________
    Malaysia Vacations
    Malaysia Holiday

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. CSS? - background color doesnt stay the same
    By kshare in forum Programming Help
    Replies: 9
    Last Post: 12-07-2009, 12:55 AM
  2. selectbox background color
    By galaxyAbstractor in forum Programming Help
    Replies: 3
    Last Post: 08-26-2009, 04:16 PM
  3. Replies: 3
    Last Post: 12-01-2008, 02:40 PM
  4. how do u change background color??/
    By Not_My_Problem in forum Graphics & Webdesign
    Replies: 5
    Last Post: 01-16-2008, 10:01 PM
  5. How Do I Change The Color Of.....
    By FabioRibeiro in forum Free Hosting
    Replies: 0
    Last Post: 01-04-2008, 03:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers