61 users online (1 members and 60 guests)  


  Results 1 to 5 of 5

Related

  1. Replies: 1
  2. document.getElementById    Forum: Javascript Forum
    Replies: 0
  3. Replies: 0
  4. Mutiple element id's    Forum: Javascript Forum
    Replies: 2
  5. document.getElementByID    Forum: Javascript Forum
    Replies: 6
  1. #1
    MdBenner's Avatar
    New User

    Status
    Offline
    Join Date
    Dec 2007
    Posts
    12

    How to use getElementById with a body element

    Hi everyone.

    I am working on an AJAX based project, and there is just one little problem i have using the getElementById method:

    It is the following:

    If I use the following code, everything works as expected:

    document.getElementById("body").bgColor='#ff5477';

    However instead of changing the background color, I want to change the background-image that is used.

    I've seen various sites that say you can do so by using the background property:

    document.getElementById("body").background=???

    However, I was not able to figure out the correct syntax to go where the three questionmarks are. This means I just never got it to do anything. Can anybody help me?

    Thanks a lot, Simon

  2. #2
    vinyl-junkie's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    721

    Re: How to use getElementById with a body element

    Here you go:

    Code:
    document.bgColor='#ff5477';
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more

  3. #3
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: How to use getElementById with a body element

    Hi there MdBenner,

    you should not use bgcolor or background as they are deprecated attributes.

    Source:-
    Instead try it like this....
    Code:
    
    document.getElementById('foo').style.backgroundColor='#ff5477';
    ...and this...
    Code:
    
    document.getElementById('foo').style.backgroundImage='url(your_image.jpg)';
    Further reading:-
    If you are applying styles to the body element then you do not need to use
    document.getElementById
    as document.body will suffice.

    Code:
    
    document.body.style.backgroundImage='url(your_image.jpg)';
    Last edited by coothead; 12-09-2007 at 08:05 AM.

  4. #4
    MdBenner's Avatar
    New User

    Status
    Offline
    Join Date
    Dec 2007
    Posts
    12

    Re: How to use getElementById with a body element

    Thanks so much...

  5. #5
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: How to use getElementById with a body element

    No problem, you're welcome.



Tags for this Thread