473,387 Members | 1,517 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,387 software developers and data experts.

Checking if element is in the node of another element

abs
Has anybody seen a function which checked if one of passed elements is
nested in the node of another element passed to function, no matter how deep
it is nested ?

Thanks in advance,
ABS
Jul 23 '05 #1
3 1419
abs wrote:
Has anybody seen a function which checked if one of passed elements is
nested in the node of another element passed to function, no matter how deep
it is nested ?


No. The following expects to be passed a reference to two elements.
If y is a descendent of x, it returns true. Otherwise, false.

function isDescendent(x, y){
while ( y = y.parentNode ) {
if ( y == x ) return true;
}
return false;
}


--
Rob
Jul 23 '05 #2
RobG <rg***@iinet.net.auau> writes:
function isDescendent(x, y){
while ( y = y.parentNode ) {


Just for safety, I recommend writing this as:
while ( (y = y.parentNode) ) {
just to make it explicit that you really mean an assignment and
not a comparison. Otherwise, it's just a matter of time before
some misguided coder puts this code into a
<script language="JavaScript1.2">...</script>
element and has it break in Mozilla ... because JavaScript version 1.2
actually "fixed" code like this to be a comparison.

/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 #3
abs wrote:
Has anybody seen a function which checked if one of passed elements is
nested in the node of another element passed to function, no matter how
deep it is nested ?


Yes.
PointedEars
Jul 23 '05 #4

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

Similar topics

0
by: Richard Lewis | last post by:
Hey, Can I convert an xml.dom.Node object to an xml.dom.Element object? I want to do the conversion inside a condition like this: if node.nodeType == Node.ELEMENT_NODE: # do conversion:...
3
by: Y.S. | last post by:
Hello All, I have the following schema: .... <xs:element name="data_type_id" type="xs:unsignedShort" /> <xs:element name="data_value" type="xs:anyType" /> .... What I want to do is to...
0
by: Piers Chivers | last post by:
Hi, I need to restrict the allowed included elements dependent upon the value of another element. How do I do this using XML Schema? For example, the XML <Petshop> <Kind>Dog</Kind>...
6
by: ste.paoletti | last post by:
hi, I have read that browsers support block element inside inline element but the css is not valid. Someone can tell me more about? What do you think? Thanks.
5
by: felipevaldez | last post by:
how can I put an element below another element, that's not absolutely positioned? so I have a n element, called X, and next to that, an element Y X Y XXXXXX
1
by: sayid | last post by:
Hello, I have a problem when altering a DIV-element from another frame. The frameset looks like this: <frameset rows=0,0,* border=0> <frame name=header src=/header.htm scrolling=no...
4
Frinavale
by: Frinavale | last post by:
Is there a method like document.getElementByID() which will retrieve an element from another DOM element? Rephrasing: Say I retrieve a Div element using the getElementID method, is there a method...
35
Frinavale
by: Frinavale | last post by:
I have a quick question... Before I start looping through all of the child elements of a <div> I wanted to ask if there is a better way to check if an element is a child of another element? I...
5
by: mruedy | last post by:
I would like to know if it is possible to write an XML Schema restriction that binds to values of another element. For example I have XML <people> <family name="Smith"> ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.