Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > HTML > When do elements get their style attribute values?

Reply
Thread Tools

When do elements get their style attribute values?

 
 
Tim Streater
Guest
Posts: n/a
 
      07-11-2011
There are some elements in one of my app's pages whose height values I'd
like to get as soon as possible, since I want to use the values to
calculate and set heights of other elements. And I'd like to be able to
either get the element's natural height or one I set using CSS.

So, I'm doing something like [1]:

h =
document.defaultView.getComputedStyle(e,null).getP ropertyValue("height");

where e is a pointer to my element.

But I find that if I do this right at the start of the onload function,
I get back a zero value, whereas if I do it a bit later (e.g. after
waiting for some ajax calls to complete), then I get the proper value.

So when are these style values set? The reason for this is to avoid what
I do at the moment: compute the true height by hand and just have a
JavaScript variable initialised to that value.


[1] If/when I can sort this I can collect the border/margin/padding
values too.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
 
Reply With Quote
 
 
 
 
richard
Guest
Posts: n/a
 
      07-11-2011
On Mon, 11 Jul 2011 16:24:27 +0100, Tim Streater wrote:

> There are some elements in one of my app's pages whose height values I'd
> like to get as soon as possible, since I want to use the values to
> calculate and set heights of other elements. And I'd like to be able to
> either get the element's natural height or one I set using CSS.
>
> So, I'm doing something like [1]:
>
> h =
> document.defaultView.getComputedStyle(e,null).getP ropertyValue("height");
>
> where e is a pointer to my element.
>
> But I find that if I do this right at the start of the onload function,
> I get back a zero value, whereas if I do it a bit later (e.g. after
> waiting for some ajax calls to complete), then I get the proper value.
>
> So when are these style values set? The reason for this is to avoid what
> I do at the moment: compute the true height by hand and just have a
> JavaScript variable initialised to that value.
>
>
> [1] If/when I can sort this I can collect the border/margin/padding
> values too.


I think your question would be more suited in a javascript group.
Posting a link to a working example wouldn't hurt.
Personally, I wouldn't use JS to set style values.
 
Reply With Quote
 
 
 
 
Tim Streater
Guest
Posts: n/a
 
      07-11-2011
In article <(E-Mail Removed)>,
richard <(E-Mail Removed)> wrote:

> On Mon, 11 Jul 2011 16:24:27 +0100, Tim Streater wrote:
>
> > There are some elements in one of my app's pages whose height values I'd
> > like to get as soon as possible, since I want to use the values to
> > calculate and set heights of other elements. And I'd like to be able to
> > either get the element's natural height or one I set using CSS.
> >
> > So, I'm doing something like [1]:
> >
> > h =
> > document.defaultView.getComputedStyle(e,null).getP ropertyValue("height");
> >
> > where e is a pointer to my element.
> >
> > But I find that if I do this right at the start of the onload function,
> > I get back a zero value, whereas if I do it a bit later (e.g. after
> > waiting for some ajax calls to complete), then I get the proper value.
> >
> > So when are these style values set? The reason for this is to avoid what
> > I do at the moment: compute the true height by hand and just have a
> > JavaScript variable initialised to that value.
> >
> >
> > [1] If/when I can sort this I can collect the border/margin/padding
> > values too.

>
> I think your question would be more suited in a javascript group.


Not necessarily. This is basically a question about when DOM element
styles become accessible.

> Posting a link to a working example wouldn't hurt.


No URL is available as in this case, the client and server are on the
same machine. The only URL would be localhost which is not too useful.

> Personally, I wouldn't use JS to set style values.


The user has the ability to grab a particular element (a species of
grab-bar) with the mouse and expects to be able to move it up (down). To
effect this, the element above needs to get shorter (taller) and the
element below needs to get taller (shorter). At the moment I do this
with JavaScript, suggestions for alternatives welcome.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When do elements get their style attribute values? Tim Streater Javascript 24 07-18-2011 05:30 AM
Control of CSS pseudo elements through a STYLE attribute in .NET framework Kevin Frey ASP .Net 0 07-28-2006 07:17 AM
drag a box over renderd elements in browser and get their xpath Sebastian Fey Javascript 0 04-27-2006 11:31 PM
picking value of one attribute based on a child elements attribute? XPath? vjethava@gmail.com XML 2 03-06-2006 05:19 AM
Using "visibility" style attribute with select elements Dave Hammond Javascript 2 02-18-2005 11:06 PM



Advertisments