473,396 Members | 2,011 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,396 software developers and data experts.

suggest a function that will spit out the dom path

can anyone suggest a function that will spit out the dom path for an
object on id?
I'm having troubles targettting elements
so i thought, if i make a function that spits path (you send it id) then
it would help

html has:
<div name="nav"> <a href="yo">hey</a><p>this <h1 id="io">hey</h1></p></div>

i want to do something like
gdp('io');
and
print to alert or something.. something like

document.nav.io

you know?

(please no wisecracks about how that html example is invalid )
Jul 23 '05 #1
5 2090

well, simple:
<ELEMENT id="SOMEIDHERE"> .... </ELEMENT>
document.getElementById('SOMEIDHERE');

Danny

On Fri, 24 Jun 2005 17:40:49 -0700, Jean Paul Sartre <no@way.com> wrote:
can anyone suggest a function that will spit out the dom path for an
object on id?
I'm having troubles targettting elements
so i thought, if i make a function that spits path (you send it id) then
it would help

html has:
<div name="nav"> <a href="yo">hey</a><p>this <h1
id="io">hey</h1></p></div>

i want to do something like
gdp('io');
and
print to alert or something.. something like

document.nav.io

you know?

(please no wisecracks about how that html example is invalid )


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #2
Jean Paul Sartre wrote:
can anyone suggest a function that will spit out the
dom path for an object on id?
I'm having troubles targettting elements so i thought,
if i make a function that spits path (you send it id)
then it would help

html has:
<div name="nav"> <a href="yo">hey</a><p>this <h1
id="io">hey</h1></p></div>

i want to do something like
gdp('io');
and
print to alert or something.. something like

document.nav.io

you know?
Not really. No named properties of the document would be expected to
correspond with an (invalid) NAME attribute on a DIV, and that DIV
element would not be expected to have named properties that correspond
with ID attribute values on its children.

The properties that describe the relationship between elements in the
DOM tree are:-

firstChild
lastChild
nextSibling
previousSibling
parentNode

- and the:-

childNodes

- collection.

And in those terms the path between the document and any given element
may take many forms.

document.body.firstChild.firstChild.nextSibling

- might be the same element as:-

document.body.lastChild.previousSibling.childNodes[1]

While it is useful to be aware of the DOM tree structure it is rarely
useful to be interested in absolute paths from the document element to
its descendants. It is much more likely that you need to understand
acquiring and manipulating references to DOM nodes.
(please no wisecracks about how that html example
is invalid )


They are not wisecracks. It is important when scripting a DOM that the
HTML it is created from is valid. Your example is invalid in having a
superfluous closing P tag, but it is not the mark-up that the presence
of the closing P tag implies that you think it is.

A P element may not have block level emollients such as H1 as its
children, and the closing tag of a P element is optional and may be
omitted from the source code. So the browser reads your example HTML and
finds an H1 where it appears to be inside a P and, knowing that the P
cannot contain the H1, it is in a position to infer the closing P tag.
Later it encounters the superfluous second closing P tag, but as no P
element is open at that point it can error-correct that redundant tag
out of the DOM. Thus the mark-up you have presented is actually
equivalent to:-

<div><a href="yo">hey</a><p>this </p><h id="io">hey</h1></div>

- but what appeared to be a concept of an H1 contained in a P element
has turned out to be an H1 element that is the following sibling of a P
element and a direct child of a DIV element.

If your mark-up produces a DOM structure that does not correspond with
the structure that is the apparent intent of the mark-up then you will
encounter problems trying to navigate paths through your DOM. Because
you will not be scripting the DOM you think your are scripting.

Creating valid mark-up removes many of these considerations, and results
in constantly structured DOMs (particularly cross-browser, where
error-correction techniques differ). An HTML validator would have
complained about encountering a closing P tag when no P element was
open, and that would have been your clue that the DOM you would get from
the mark-up would not be the DOM you were expecting.

That a HTML validator would also complain about the DIV having a NAME
attribute then becomes you clue that attempting to reference that DIV
with that NAME value is not going to be at all successful cross-browser
(as some will disregard its existence).

Richard.
Jul 23 '05 #3
Jean Paul Sartre wrote:
can anyone suggest a function that will spit out the dom path for an
object on id?


no@way.com
Jul 23 '05 #4
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Jean Paul Sartre wrote:
can anyone suggest a function that will spit out the dom path for an
object on id?


no@way.com


Or, alternatively:

function path(id) {
var elem = document.getElementById(id);
var path = [];
while (elem != document.documentElement) {
if (elem.previousSibling) {
path.push("nextSibling");
elem = elem.previousSibling;
} else {
path.push("firstChild");
elem = elem.parentNode;
}
}
path.push("document.documentElement");
return path.reverse().join(".");
}

This is just a suggestion. It expects the element to exist, the DOM
implementation to be standards compliant, and the browser to be in
standards mode. Also, it's quite stupid :)

A slightly more compact result is given by:
---
function path(id) {
var elem = document.getElementById(id);
var path = [];
var ci = 0;
while (elem != document.documentElement) {
if (elem.previousSibling) {
ci++;
elem = elem.previousSibling;
} else {
path.push("childNodes["+ci+"]");
ci = 0;
elem = elem.parentNode;
}
}
path.push("document.documentElement");
return path.reverse().join(".");
}
---

Now, what on earth does the original poster need this for?!?

/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 23 '05 #5
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Jean Paul Sartre wrote:
can anyone suggest a function that will spit out the dom path for
an object on id?

no@way.com


Or, alternatively: [...]


Yes, of course. However, my point was that I do not intend to help
people (at least no second time) who disobey Internet standards (by
spoiling the global DNS namespace or abusing domains) and/or make
themselves unidentifyable by using ridiculous From headers). Funny
that my reply unintentionally bears another meaning that applies here
as well.
PointedEars
Jul 23 '05 #6

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
4
by: Andy | last post by:
What do people think of this? 'prefixed string'.lchop('prefix') == 'ed string' 'string with suffix'.rchop('suffix') == 'string with ' 'prefix and suffix.chop('prefix', 'suffix') == ' and ' ...
4
by: Reply Via Newsgroup | last post by:
Folks, I have a simple script below... I come from a programming background with PHP,C++,bash unix shell scripting so I have a rough understanding when it comes to javascript. I have written a few...
26
by: G Patel | last post by:
Hi, I'm wondering if anyone knows if the following function will function properly as a set-bit counter on non 2s complement machines (as K&R2 implies). | int bitcount(unsigned x) | { | ...
5
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file"....
2
by: news | last post by:
I just upgraded to PHP 4.4.2 on my Slackware 10.2 system. And Apache/mySQL/PHP all work great through a browser. No errors. But when I try to run a PHP script through the command line, which I...
8
by: Adam Scheinberg | last post by:
Hello all, This is my first post and I'm wondering if someone can point me in the right direction. I have seen the following code MANY times in programs and throughout the internet: <?php...
8
by: Razzbar | last post by:
I've been trying to write a general buffer function that looks something like this: function buffer($arg){ ob_start(); $arg; return ob_get_clean(); } Note that the intention is for this to...
3
by: monojohnny | last post by:
Hi, I know you can do stuff with introspection to gather up passed-in args for a Javascript function and that you can list all defined functions like: function a(abc,xyz,zzz) {...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.