|
#1
|
||||
|
||||
|
I am trying to write an "ancestor function". it will take three values child parent and stop. All three are elements. The purpose is to check whether child is located under parent in the DOM. It should only search until it reachs stop(or the parent).It will then return true or false accordingly. I hope that makes sense. Here's my attempt.
Code:
function ancestor(child,parent,stop){
if(child.parentNode){
while(child != stop){
if(child.parentNode == parent){
return true;
}
else{
child = child.parentNode;
}
}
}
return false;
}
__________________
-Brady |
|
#2
|
||||
|
||||
|
Never Mind, I figured it out. Here's the working one if you need it
Code:
function ancestor(child,parent,stop){
if(child.parentNode){
while(child != stop){
if(child.parentNode){
if(child.parentNode == parent){
return true;
}
else{
child = child.parentNode;
}
}
else{
return false;
}
}
}
return false;
}
__________________
-Brady |
|
#3
|
||||
|
||||
|
Blimey... I've heard of verbosity but that takes the biscuit
![]() Code:
function ancestor(child, parent, stop) {
for(; child.parentNode && child !== stop; child = child.parentNode)
if(child.parentNode === parent) return true;
return false;
}
__________________
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP! |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
|
|