473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problems accessing CSS attributes with JS

Hey,

I'm having an issue with CSS, JS and the DOM. It's gonna drive me batty. I
am trying to access the properties of a layer in JS which have been
initially set in an external CSS. The problem is that for some reason JS
isn't receiving NULL for all the properties of the layer. For instance:

HTML
<div id="blarg">.....</div>

JS
var t = document.getElementById("blarg").style.left;

CSS
#blarg{
position: absolute;
left: 200px;
}

t == NULL and I can't figure out why. It is this way for any CSS attribute
I attempt to read (width, positional, the whole kit and caboodle).

It does work if I set the attribute in JS before I read it.

JS
document.getElementById("blarg").style.left = 30;
var t = document.getElementById("blarg").style.left;

t now == 30. The only thing I can think of to explain this is that when
the document gets loaded my layers aren't getting their attributes filled
out. The object gets created and the attribute fields are blank until I
fill them in. Thusly my creation of CSS's was a waste of time. Grr...

Any ideas/suggestions? Google has been of no help. :-( This is occurring in
IE 6, and the gecko engine
browsers.

Mike

Jul 20 '05 #1
4 2003

"Mike Clair" <th************@BLARGshaw.ca> wrote in message
news:f7P_b.594347$JQ1.432494@pd7tw1no...
Hey,

I'm having an issue with CSS, JS and the DOM. It's gonna drive me batty. I
am trying to access the properties of a layer in JS which have been
initially set in an external CSS. The problem is that for some reason JS
isn't receiving NULL for all the properties of the layer. For instance:

HTML
<div id="blarg">.....</div>

JS
var t = document.getElementById("blarg").style.left;

CSS
#blarg{
position: absolute;
left: 200px;
}

t == NULL and I can't figure out why. It is this way for any CSS attribute
I attempt to read (width, positional, the whole kit and caboodle).

It does work if I set the attribute in JS before I read it.

JS
document.getElementById("blarg").style.left = 30;
var t = document.getElementById("blarg").style.left;

t now == 30. The only thing I can think of to explain this is that when
the document gets loaded my layers aren't getting their attributes filled
out. The object gets created and the attribute fields are blank until I
fill them in. Thusly my creation of CSS's was a waste of time. Grr...

Any ideas/suggestions? Google has been of no help. :-( This is occurring in IE 6, and the gecko engine
browsers.

Mike


Mike,

Styles and standard HTML attributes are not filled out. There are some
dynamic properties that are determined when rendering the page (offsetLeft,
offsetTop, offsetWidth, offsetHeight come to mind but that might be only in
IE) but most other properties like "width", "height" (both the CSS styles as
the HTML attributes) are not dynamic in that sense.

Silvio Bierman
Jul 20 '05 #2
"Silvio Bierman" <sb******@idfix.nl> wrote in message
news:40*********************@news.xs4all.nl...


Mike,

Styles and standard HTML attributes are not filled out. There are some
dynamic properties that are determined when rendering the page (offsetLeft, offsetTop, offsetWidth, offsetHeight come to mind but that might be only in IE) but most other properties like "width", "height" (both the CSS styles as the HTML attributes) are not dynamic in that sense.

Silvio Bierman


Thanks for the super timely response. :-)

I need these attributes to be dynamic because I have a course assignment to
implement drag and drop in JS. I suppose the only way to do this would be to
fill out the properties with JS.

Is there any way to force the browser to fill out these properties when
rendering? (Prolly not cuz if there was you would have told me)

If not, I guess I'll just set the page up with an initialization method
onLoad. An external CSS was more elegant though. If you have any other ideas
on how I can implement this differently I'm very welcome to suggestions.

Thanks.

Mike
Jul 20 '05 #3
"Mike Clair" <th************@BLARGshaw.ca> writes:
I'm having an issue with CSS, JS and the DOM. It's gonna drive me batty. I
am trying to access the properties of a layer
Layer? Ah, you mean a named division, not the Netscape 4 tag <layer>.
in JS which have been initially set in an external CSS. The problem
is that for some reason JS isn't receiving NULL for all the
properties of the layer. For instance:

HTML
<div id="blarg">.....</div>

JS
var t = document.getElementById("blarg").style.left;
This accesses the style object of the blarg element. It corresponds to
the style assignments made in the style attribute of HTML tag. It does
not reflect the effective style of the element or the global stylesheets.

To get the actual style, use either the W3C DOM standard:
var blarg= document.getElementById("blarg")
var t = document.defaultView.getComputedStyle(blarg,"").le ft;
(works in Mozilla and recent Opera) or the proprietary IE method:
var t = document.getElementById("blarg").currentStyle.left ;
It does work if I set the attribute in JS before I read it.
Yes. You assign it to the element's style object, which has the
highest priority of CSS styles (except the user's !important rules),
so it takes effect and is accessible from that object again.
t now == 30.
I would hope it is "30px", and a string.
Any ideas/suggestions? Google has been of no help. :-( This is
occurring in IE 6, and the gecko engine browsers.


It is correct behavior.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
Thanks. I guess I should have thought of the CSS style hierarchy, oh well.
That solved my problem nicely.

Mike

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:sm**********@hotpop.com...
"Mike Clair" <th************@BLARGshaw.ca> writes:
I'm having an issue with CSS, JS and the DOM. It's gonna drive me batty. I am trying to access the properties of a layer
Layer? Ah, you mean a named division, not the Netscape 4 tag <layer>.
in JS which have been initially set in an external CSS. The problem
is that for some reason JS isn't receiving NULL for all the
properties of the layer. For instance:

HTML
<div id="blarg">.....</div>

JS
var t = document.getElementById("blarg").style.left;


This accesses the style object of the blarg element. It corresponds to
the style assignments made in the style attribute of HTML tag. It does
not reflect the effective style of the element or the global stylesheets.

To get the actual style, use either the W3C DOM standard:
var blarg= document.getElementById("blarg")
var t = document.defaultView.getComputedStyle(blarg,"").le ft;
(works in Mozilla and recent Opera) or the proprietary IE method:
var t = document.getElementById("blarg").currentStyle.left ;
It does work if I set the attribute in JS before I read it.


Yes. You assign it to the element's style object, which has the
highest priority of CSS styles (except the user's !important rules),
so it takes effect and is accessible from that object again.
t now == 30.


I would hope it is "30px", and a string.
Any ideas/suggestions? Google has been of no help. :-( This is
occurring in IE 6, and the gecko engine browsers.


It is correct behavior.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors:

<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jon Dellaria | last post by:
I have been using MySql as the database using JSP's and JavaBeans but recently I have wanted to start using the database connection pooling mechanism built into TomCat. I think I am having a...
2
by: Marc A. Criley | last post by:
First off, I'm an experienced software developer but new to XML, so be gentle :-) I've put together a schema that defines optional attributes for some of the elements. Some of these attributes...
1
by: wooks | last post by:
I have some information embedded in included schemas which I want to access at run-time for the purposes of contructing a GUI (they will support field labels and tool tips). The options seem to...
2
by: Erwin S. Andreasen | last post by:
Hi, I have a web application where window A opens window B (same site). B later wants to do something depending on whether the window A, window.opener.document.domain, has changed. However,...
1
by: Jesper Stocholm | last post by:
I have som XML like this: <root> <Course CourseCode="id1"> <Teacher Name="Some name"/> <Title Titlde="Dansk Titel 1"/> <Title Title="English Title 1"/> <Location Place="Some place"/>...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
3
by: Musravus | last post by:
Hi all, I have a piece of XML that looks like this, which I want to DataBind to a Repeater (the Repeater is inside a UserControl): <SECTION id="MAIN" > <SUBSECTION id="SUB1" /> <SUBSECTION...
3
by: Beorne | last post by:
In the classes I develop my attributes are always private and are exposed using properties. directly or to access the attributes using the properties? Does "wrapper" setter/getter properties...
3
by: John Dann | last post by:
Trying to learn Python here, but getting tangled up with variable scope across functions, modules etc and associated problems. Can anyone advise please? Learning project is a GUI-based...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.