PDA

View Full Version : scope in javascript (this._parent)?????



3pepe3
December 18th, 2008, 05:36 AM
Hello world,

I'm an actionscripter, and now i'm testing with javascript.

for example in flash i have mc.mc1.mc2... and if i'm on the scope of mc2 i can use this._parent._parent to reach the scope of mc.
in javascript i can't find the way to reach the parent of an element...

Example:
I have a line like this:

<ul>
<li><label for="code_postal" class="element">ble :</label><div class="element"><input id="myNode" type="text" /></div></li>
<li>...</li>
<li>...</li>
</ul>

then in javascript my scope is this:

var el=document.getElementById("myNode");
How can i reach the li tag of this element?

Thanks
Pepe

3pepe3
December 18th, 2008, 07:28 AM
Well i think that one of the ways to solve this would be to loop and find the desired element...

but do several loops each time that the users do sommething is not ok for me...
so i added an id to the posible elements that i wanted.
something like this:


function setID(parentNode){
var setId=["sujet1","nom", "prenom", "adresse", "ville", "code_postal", "email", "telephone"]
var el= document.getElementById(parentNode);
var nodeList = el.getElementsByTagName("li");
for(var i=0; i<nodeList.length; i++){
nodeList[i].id=setId[i]+"_li";
}
}
now the i access to the li tags more easy...
Any others ways to solve this?

Sage_of_Fire
December 18th, 2008, 08:32 AM
Yep. You can use the parentNode property on el (from the first script) to get its container. Isn't the DOM great? :lol: You can also check out the Mozilla Developer Center (https://developer.mozilla.org/en/JavaScript) to see all the cool DOM objects, methods, and properties.