SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Location
    Taipei, Taiwan
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using JS to set height/width

    I am having a problem with trying to set a div's width and height using Javascript.

    The javascript I am using is this:
    Code:
        obj = document.getElementById('gameDiv');
        obj.style.height = 100;
        obj.style.width = 100;
    I have a div with ID of gameDiv. It already has its height/width set by default using CSS, so the value of style.height and style.width aren't null.

    The code doesn't bring up any errors. But for some reason, it doesn't change the height/width. I'm thinking this might be because I can't use JS to update style attributes...

    Does anybody know how I can change the width/height of a div using javascript? (A fix to this code or an alternative?)

    Thanks!

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You're missing the units. '100px' instead of 100. The style object is like CSS, so you always write values for the style properties like you would in CSS.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Location
    Taipei, Taiwan
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that worked perfectly!


Bookmarks

Posting Permissions

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