473,395 Members | 1,404 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.

Finding an element position

I'm having an odd type of issue. I want to be able to pass an element
name in my javascript event and find the location of the element, be it
a div, span, img whatever, specifically the top and left attributes.

I have defined my element like so...

### .css file...

#mydiv {
position:absolute;
top:100px;
left:100px;
}

### .html file...

<div id="mydiv" ><a href="#" onMouseUp="myAction()">Some
Content</a></div>
<div id="test"></div>

### and in my .js file I call to test the position..

var divstyle = document.getElementById("mydiv").style;
var left = divstyle.left;
var top = divstyle.top;

document.getElementById("test").innerHTML += "<BR>left="+left;
document.getElementById("test").innerHTML += "<BR>top="+top;

If I do this, left and top show as undefined. If I change up the html
to call a style tag directly, like this..

<div id="mydiv" style="top:100px;left:100px;">....

Then it works just fine. I'm not sure I'm understanding why these two
css definitions are responding differently. I'd rather not have to
define the location of all my elements directly in the html, preferring
to rely on definitions in my css files.

Am I missing something? Is there an alternate way of finding an
elements position?

Feb 20 '06 #1
4 6332
sp****@gmail.com wrote:
I'm having an odd type of issue. I want to be able to pass an element
name in my javascript event and find the location of the element, be it
a div, span, img whatever, specifically the top and left attributes.

I have defined my element like so...

### .css file...

#mydiv {
position:absolute;
top:100px;
left:100px;
}

### .html file...

<div id="mydiv" ><a href="#" onMouseUp="myAction()">Some
Content</a></div>
<div id="test"></div>

### and in my .js file I call to test the position..

var divstyle = document.getElementById("mydiv").style;
var left = divstyle.left;
var top = divstyle.top;
What you get here are the top and left attributes of the div's style
object, you don't get style settings that are inherited from CSS styles.

document.getElementById("test").innerHTML += "<BR>left="+left;
document.getElementById("test").innerHTML += "<BR>top="+top;

If I do this, left and top show as undefined. If I change up the html
to call a style tag directly, like this..

<div id="mydiv" style="top:100px;left:100px;">....

Then it works just fine. I'm not sure I'm understanding why these two
css definitions are responding differently. I'd rather not have to
define the location of all my elements directly in the html, preferring
to rely on definitions in my css files.


Start at quirksmode.org:

<URL:http://www.quirksmode.org/js/findpos.html>


--
Rob
Feb 20 '06 #2
RobG wrote:
Then it works just fine. I'm not sure I'm understanding why these
two css definitions are responding differently. I'd rather not have
to define the location of all my elements directly in the html,
preferring to rely on definitions in my css files.

Start at quirksmode.org:
<URL:http://www.quirksmode.org/js/findpos.html>


Or my code, which actually provides more accurate results in a variety of
environments:
http://www.JavascriptToolbox.com/lib/objectposition/

But really, I don't think either of those are what you really want.

Instead, you need to understand that an object's .style attribute only
refers to what has been declared inline, via the "style" attribute.
To get style attributes which have been applied using non-inline styles,
check into getComputedStyle (standard) and currentStyle (IE) to get what you
want.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Feb 20 '06 #3
sp****@gmail.com wrote :
I'm having an odd type of issue. I want to be able to pass an element
name in my javascript event and find the location of the element,
The location of the element related to what exactly? to which coordinate
system? related to user screen? related to client area (browser/user
agent viewport)? related to document? related to document view? related
to root element? related to body element? related to positioned
containing block? related to offsetParent node? related to parent
element? related to relatively positioned element within the containment
hierarchy?
be it a div, span, img whatever, specifically the top and left attributes.

I have defined my element like so...

### .css file...

#mydiv {
position:absolute;
top:100px;
left:100px;
}

### .html file...

<div id="mydiv" ><a href="#" onMouseUp="myAction()">Some
Content</a></div>
<div id="test"></div>

### and in my .js file I call to test the position..

var divstyle = document.getElementById("mydiv").style;
var left = divstyle.left;
var top = divstyle.top;

document.getElementById("test").innerHTML += "<BR>left="+left;
document.getElementById("test").innerHTML += "<BR>top="+top;

If I do this, left and top show as undefined.
IMO, you got the good answer from the browser: undefined. The <div
id="test"> was not absolutely positioned.
If I change up the html to call a style tag directly, like this..

<div id="mydiv" style="top:100px;left:100px;">....

Then it works just fine.
It's misleading since the <div id="mydiv">'s position is static, not
absolute, nor relative.

I'm not sure I'm understanding why these two css definitions are responding differently. I'd rather not have to
define the location of all my elements directly in the html, preferring
to rely on definitions in my css files.

Am I missing something? Is there an alternate way of finding an
elements position?


It all depends on what is the referenced coordinate system. Right now,
I'm 2 feet away from my PC computer. But depending on what is your
referenced coordinate system, you could say I'm 1000 miles from London.
Again, it all depends on the referenced coordinate system to begin with.

Gérard
--
remove blah to email me
Feb 21 '06 #4
Matt Kruse wrote:
But really, I don't think either of those are what you really want.

Instead, you need to understand that an object's .style attribute only
refers to what has been declared inline, via the "style" attribute.
To get style attributes which have been applied using non-inline styles,
check into getComputedStyle (standard) and currentStyle (IE) to get what you
want.
Thanks Matt and Rob. I think this is exactly what I was looking for. I
appreciate the help. I'm adding both your sites to my bookmarks for
future review. I've got a lot of learning to do.

Gerard Talbot wrote :
The location of the element related to what exactly? to which coordinate
system? related to user screen? related to client area (browser/user
agent viewport)? related to document? related to document view? related
to root element? related to body element? related to positioned
containing block? related to offsetParent node? related to parent
element? related to relatively positioned element within the containment
hierarchy?


Gerard, I appreciate that maybe you were trying to be helpful, but I'm
quite new at this and if my examples didn't provide enough information
for you to get the gist of what I was trying to do, I don't think I can
make it any clearer. These are probably issues I should consider, but
I had positioned my div originally as 'absolute' (although I'll admit
when I put the style tag inline on my example I forgot to position it
absolute again) and all your questions seem to be assuming I had placed
the position as 'relative' to something else. I wasn't trying to be
tricky, I was being simple in my example. I wasn't trying to find any
difficult relative position, I was just curious as to why my js style
calls were only finding the in-line style tags. I think Matt and Rob
understood and answered my question sufficiently.

Feb 21 '06 #5

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

Similar topics

3
by: Jamie Green | last post by:
Using MSXML3.0, with the Dom SelectionLanguage set to Xpath I am trying to query the following document <Root> <Child>Name</Child> <Child>John</Child> <Child>Smith</Child> <Child>23</Child>...
1
by: Eshrath Ali Khan | last post by:
Hi, I have a requirement where I am transforming a XML into a html using an XSL. I need to create a object for the each and every occurrence of an element named "tgroup" in the xml. Also I need...
2
by: Pallavi | last post by:
Hello, Suppose I want to highlight a piece of text. I get the x & y co-ordinates on the screen. And using dhtml I am able to highlight the text. Then I try on another machine with different...
20
by: Webdad | last post by:
Hi! I running my first year as industrial engineer (informatics) We have an assignment to do : .... create a playfield (matrix). Some places in that field are blocked, so you can't pass them....
2
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
2
by: ElkGroveR | last post by:
Hi there! I'm using PHP to create a simple, dynamic MySQL SELECT query. The user chooses a selection from a HTML Form SELECT element's many options and submits the form via a POST action. ...
0
by: guile | last post by:
hi, i have 2 tables: one for a package, and other for the items in that package. item table has item_id as primary key (set on auto_increment), and a foriegn key package_id that links to the...
15
by: rhino | last post by:
I've put together a prototype of two-tiered CSS tabs that works really well in IE6, IE7, and FF2. It also works very well in Opera 9.27 _except_ that the placement of the lower tier of tabs is...
3
by: madshov | last post by:
Hi, I have the following: <start> <segment Id="AAA"> <element Id="id">1</element> <element Id="seq">122</element> <element Id="seq2" Composite="yes">
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.