The Identification Number and Password fields need to be filled in. Moreover, you cannot send messages devoid of contents Adding further data is not allowed because this topic has been closed Print You provided no password: you have to write it in the textfield beside the command delete for the topic you want to delete Are you sure you want to delete this comment? You did not cast a vote. You need to cast a vote by selecting a radio button. Impossible to proceed Provide the project number Insufficient form parameters: you forgot to fill in some field/s
 
member picmember pic
 
Snippets of A
 
info Below you can find the text of the snippet you want to read, and the list of the other snippets by this author if available.
What are snippets?
Share on MySpace

Javascript DOM Get Parent Nodes: All Nodes Or Specific Node By Id Or By Tag Name

Snippet: Identification Number »   Snippet: Inclusion syntax »
Visitors: 48,418 Tagged by its author as: Programming Javascript Characters (in origin): 3,048 (pages: ~ 1)
Author: Em@il Permalink Cast your vote for this topic Printable version
OBJECTIVE: given a dom node (html tag) find its parent nodes: either all or until a specified one is located.  
Note: this procedure returns parent nodes only: if you want all the nodes you may check Javascript Scan Arrays Matrix, Dom Trees, Tag Attributes. Map Or Find Values.  
 
This process could be useful for instance also in those cases where you want to verify whether some event handler has been triggered from within a specific wider context: for instance if an onclick upon an image has been performed upon an image nested within, say, a DIV tag with some specific id or className or whatever.  
function getParent(element, parent){
if(typeof element=="string"){element=document.getElementById(element);};
if(!element){return null;};
var elements=[];
if(typeof parent!="string"){/*no parent: gets all parents till #document*/
	while(element.parentNode){
	element=element.parentNode;
	elements.unshift(element);
		if(element==parent){return elements;};
	}
}
else{/*string, presumes you want to locate the first parent node that is such TAG*/
parent=parent.toUpperCase();
	while(element.parentNode){
	element=element.parentNode;
	elements.unshift(element);
		if(element.nodeName && element.nodeName.toUpperCase()==parent){return elements;};
	}
};
return elements;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
INPUTS: first the node whose parents you want to find, passed as an object either already grabbed via document.getElementById or as the string id of the node.  
Then as second input either nothing (it reports all parent nodes), or an already grabbed node via document.getElementById, or a string: in this last case it is presumed the string is the first tag name (instance: "DIV") that you want to locate as parent of the first passed node.  
 
RETURNS: null if the input was not a valid DOM node, or an array holding all the parent node objects traversed in the process, with the outermost nodes starting at index 0. Therefore if you were looking for one specific node, it will be always located at entry 0 if found.  
Since the items held by the array are valid DOM objects, you can execute read or write any of them using DOM properties or methods.  
 
Returns empty array if no (such) parent node could be located.  
 
CAVEATS: if you grab all the parent nodes, namely you do not pass the second input argument, the topmost item in the returned array will be: #document.  
It is advisable that your html does not include nesting errors or misplaced settings such as, for instance, LI tags without OL or UL containing tags.  
 
Example:  
<div id="outerdiv">
<div id="innermostdiv" class="divin">
<ul>
<li id='foo'>blah blah</li>
</ul>
</div>
</div>

<script>
var foundnodes=getParent('foo', 'DIV');

alert(foundnodes[0].nodeName+' '+foundnodes[0].id+' '+foundnodes[0].className);
</script>
Remove colors
Printable version To exclude hyperlinks from the print, check the checkbox »
Rss

Cast your vote for this topic

To perform this operation it is not necessary to be Full Poster members.
«Negative Positive»
Click here to save your vote (Current average: 6.50, Voters: 2)
Are you the author of this topic and do you want to append quickly more text? click here
Other topics
This subscriber has a blog too: Read the blog: 57 Shoutbox: 103 reviews: 153
Visitors: 48,418
Overall visits to all the topics: 4,957,151
Daily average (Calculated from the website subscription day): 2,059.47
Optional sorting commands:
Normal order: click here
Order by amount of visits: click here
Order by category: Programming Javascript
Current order: normal
Other categories available for this author (Limited data report: 100):
Advice: Martial Arts and Self Defense(4), Books(236), Critical Reviews and Essays(4), Dictionaries(1), Emergency Care(4), Epistles Letters and Advice(5), Fantasy Epics and Fables(1), History and Documents(5), Humor and Jokes(2), Methematics(2), Music and Lyrics(6), News Digests and Press Reviews(1), Novels Poetry and Stories(3), Philosophy Reviews(7), Poetry(1), Programming(4), Programming Ajax(2), Programming Javascript(82), Programming Php(52), Psychology(1), Quotes(1), Religion Esoterica and Spirituality(12), Scientifical Reviews(4), Self Improvement(1), Sport Activities and Apparels(2), Tarots(1)
Showing topics: 1, 10
Available total: 448
View only a list of the snippets by this author: click here.
Other topics available for this author: click on any title below to view the complete item:
Fighting Competently: Anticipation, And Remember It's In His Eyes Identification Number: 465 Visitors: 307 La Musa Segreta: Superiorità Onnipervasiva Della Boxe Identification Number: 464 Visitors: 1,184 Ha Senso Il Doping Sportivo? Effetto Matteo Nello Sport E Business Sportivo Identification Number: 463 Visitors: 1,420 The Musicians Within The Music Box And Other Hereafter Stories Identification Number: 462 Visitors: 2,676 Freud And Jung In A Nutshell: Three Or So Shots At Psychoanalysis For Dummies Identification Number: 461 Visitors: 4,134 Division The Math Of Gods: Ambiguities Of Antanairesis And New Math Operations Identification Number: 460 Visitors: 4,371 The Meaning Of Cruelty Identification Number: 459 Visitors: 4,780 Dÿanèra Ad Eleusi: La Folgorazione Ontologica: Il Pensare Sistematico E Non Identification Number: 458 Visitors: 5,585 Creative Writing: How To Write A Novel. Best Tips From The Bester Professionals Identification Number: 457 Visitors: 7,387 Newsreel Pseudo Intel: The Middle East Approaching The New 20s Identification Number: 456 Visitors: 7,364
External services
This page of this subscriber uses external services: Hide