Topic: multiple parentNode (s) (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=10799" title="Pages that link to Topic: multiple parentNode (s) (Page 1 of 1)" rel="nofollow" >Topic: multiple parentNode (s) <span class="small">(Page 1 of 1)</span>\

 
DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

IP logged posted posted 08-30-2002 00:49 Edit Quote

OK, I have this page, and inside it contains a complicated table inside a <div>. I plan to assign an image inside one of the tables (inside a table) as a control to do something with the parent DIV. Easy enough, it seems, and yes, I do have a snippet that works, but.. er, it looks frigging *hideous*! I have this line for instance, to alter the width of that div...

this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.width = x;

I imagine you'll all agree that a string such as this doesn't seem very elegant, does it? I'd love to find that there's some hack like parentNode[9] or something, but that doesn't seem to work for me. Does anyone know how this all works, and offer a variant I could use? I'm trying top avoid using "id"'s for every object, as there may end up being many of these beasts written into one page. TIA! I'm off to keep digging for the answer myself, but would love some help if you can give it.

Your pal, -doc-

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

IP logged posted posted 08-30-2002 01:00 Edit Quote

Hmm, I'm not sure if there *is* a much more elegant solution. XPath would certainly be helpful here... =)

But let's see if there's anything in the DOM Technical Reports ( http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ )...

Ugh... doesn't look like there is anything.

Well, you can always make a function like this:

function getAncestorAtLevel(thenode,level)
{
for (var a=0; a<level; a++)
{
thenode = thenode.parentNode;
}
return thenode;
}

Which can be used like
getAncestorAtLevel(this,9)

But that's the best I can really come up with...

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

IP logged posted posted 08-30-2002 10:14 Edit Quote

or you could take lots of acid.

*passes the acid*

seriously, I don't think there is anything you CAN do, I'm with slime on the 'function for deeper levels' plan... it's not like you're looking for multiple children on one node, it's mode akin to typing '../' in a path...

I guess if you pass thru, around, or onto a particular DIV a lot, you could put an ID on *it* and use it as a jumping off point...

*shrug*

...wish I had a better answer, but I'm busy myself writing horrid Perl...


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

IP logged posted posted 08-30-2002 10:15 Edit Quote

hehehe... "Year 2002: DocOzone pioneers Obfuscated DOM"


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

andy_j
Nervous Wreck (II) Inmate

From: uk
Insane since: Aug 2002

IP logged posted posted 09-01-2002 04:02 Edit Quote

This is intersting. If you are not using document.getElementById because you have no Id in the div tag and you do not know where the element appears in the array, i think there are a couple of options

The createNodeIterator and treeWalker methods of the DOM level 2 Traversal would do the trick as you could filter out your target elements (div) and traverse through until you found it. (least i think thats how it works ). I dont think this is implemented yet though in any browsers or if so then its only the very latest (shame as it would be very usefull)

The other way i thought of was with the example below. It loops through to check for a specific tag type. It will start at your current element and work up through the elements until it matches the first target element (DIV).. (similar to Slime's, except that you dont need to specify a parent level)

It will only work however, f the element you want to access (the Parent DIV in this case) is the first that the function comes to. If there were several divs (eg around your image) then it wouldnt work.. Any other tags would be ok and you could have ten tables nested and it would still find the target div.

heres the function



Post Reply
 
Your User Name:
Your Password:
Login Options: Remember Me On This Computer
 
Your Text:
Loading...
Options: Show Signature
Enable Slimies
Enable Linkwords

« BackwardsOnwards »

Show Forum Drop Down Menu