Changing Background Image at RunTime

Discussion in 'JavaScript' started by x2i, Jul 22, 2006.

  1. #1
    Ok so this is so so simple yet I cannot remember how it is done - shoot me! :(

    lets say for instance I had a button that when clicked, changed the background image for the main document.

    Thus allowing the user to choose a background image they want for my site.

    Any help :confused:
     
    x2i, Jul 22, 2006 IP
  2. giraph

    giraph Guest

    Messages:
    484
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here's an example that changes the background to the google logo. Just use a variable instead of the direct link to change it to what the user selected.
    
    <script type="text/javascript">
    function changeBg()
    {
    	
    	document.body.background = 'http://www.google.com/intl/en/images/logo.gif'
    }
    </script>
    <input type="button" value="change" onClick="changeBg();" >
    
    Code (markup):
     
    giraph, Jul 22, 2006 IP
  3. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #3
    you can also change the className of the body with the onClick event and in the new class name, have a different background image.
     
    ccoonen, Jul 22, 2006 IP