Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7002691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:57:40+00:00 2026-05-27T20:57:40+00:00

i specified a div with a ID of testBox: <div id=testBox></div> and style it

  • 0

i specified a div with a ID of testBox:

<div id="testBox"></div>

and style it in the head section :

#testBox {
        background: red;
        width: 25px;
        height: 25px;
        left: 0;
        top: 0;
            }

then at the bottom of the body,i put the JS :

var box = document.getElementById("testBox");
        console.log(box.style.left);
        console.log(box.style.width);

use the FireBug in FireFox ,but it just tell me:

it’s an empty string….

But when i put the style infomation in the div tag like this:

<div id="testBox" style="background: red;width: 25px;height: 25px;"></div>

then the JS could do its work,retrieve all the information i want

so is this the only way to get the style information whit it all inline ,or i just miss something,after all i am just new to the JS and DOM ….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:57:41+00:00Added an answer on May 27, 2026 at 8:57 pm

    When you say box.id what is returned to you is the id attribute of the box element as declared in your html.

    When you say box.style you are accessing a javascript object also created based on your markup.

    Styling attributes that are not defined inline are not used when the dom-representation of the style attribute is created.

    Here is an article which highlights this behaviour.

    However, if you use a library like jQuery you can do

    $(function(){alert($("#textBox").css("width"));});
    

    and this will give you your css value.

    UPDATE:

    Thanks to AVD for pointing me in the right direction:
    Here is a method which uses his solution but adds support for IE < 9:

    var box = document.getElementById("testBox");
    var style = window.getComputedStyle ? window.getComputedStyle(box) : box.currentStyle;
    
    alert("Height : " + style["height"]);
    alert("Width : " + style["width"]);
    alert("Left : " + style["left"]);
    

    Here is a fiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

what's the best way to force DIV to fill specified width? I have the
if I have a div using a css class below: { top: 100px; left:500px;
I am trying to move a div Left a specified amount of pixels I'm
I am trying to scroll to the top of a specific div container. Currently
I would like a div to take all the screen height, that's why I
I'm writing a widget that searches for specific keywords in a specified #content div.
When I'm using jqplot it magically injects the plot into the specified div chart1:
I'm getting full html code using WebClient. But i need to get specified div
I have an image-based map. I want to drag a div within a specified
Float:right specified for a div inside a table cell seems to have no effect

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.