0

can a javascript get the value of the div width from a css file,

if yes, then please tell me how :)

user331080
  • 37
  • 1
  • 3
  • 3
    I don't understand what you're asking. What do you mean by "from a CSS file"? Why not just query the `div`'s real width in the DOM? – Pekka Jun 23 '10 at 15:55
  • as i want to change the width of div in different templates – user331080 Jun 23 '10 at 16:00
  • 1
    Looks like you want to get the *computed style*, see: http://stackoverflow.com/questions/2531737/javascript-incapable-of-getting-elements-max-height-via-element-style-maxheight – Christian C. Salvadó Jun 23 '10 at 16:06

2 Answers2

3

CSS files TELL what to set. The DOM (Document Object Model) HAS what it is currently which is the document.

var mydiv = document.getElementById("mydiv");
var curr_width = parseInt(mydiv.style.width); // removes the "px" at the end

Makes the assumption your have a div with id="mydiv"

edit1: There are also these:

document.getElementById("mydiv").clientWidth;
document.getElementById("mydiv").offsetWidth;

Edit2: just because it will probably come up: offsetWidth will include the width of any borders, horizontal padding, vertical scrollbar width, etc.

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
  • It is possible that div does not exits on page. So, question's author can create dom element with required attributes (for css); calculate width; remove dom element. – petraszd Jun 23 '10 at 16:08
0

You're probably looking for some of the methods/properties associated with the StyleSheetList object stored in the document.styleSheets property.

See:

http://www.javascriptkit.com/domref/stylesheet.shtml

Weston C
  • 3,642
  • 2
  • 25
  • 31