Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting elements in a nested structure 1

Status
Not open for further replies.

davikokar

Technical User
May 13, 2004
523
IT
Hallo,

Having a starting point inside a nested structure I would like to get the first child of the root parent, but this child must be in the "parent-child line" with the starting point.

For example if the structure is this one:

Code:
<ul id="a">
	<li id="b1">
		<ul>
			<li id="c1"></li>
			<li id="current1"></li>
		</ul>
	</li>
	<li id="b2">
		<ul>
			<li id="c3"></li>
			<li id="c4">
				<ul>
					<li id="d1"></li>
					<li id="current2"></li>
				<ul>
			</li>
		</ul>
	</li>
</ul>

If I start searching from current1 I should get b1, if I start from current2 I should get b2.
 
I want to get only the elements in the first level of the structure. This means: b1 or b2 in the example. Now, if I start from a child of b2, I should get b2 (and viceversa: if I start from a child of b1 I should get b1).

But the child relationship may be not direct, so I can also start searching from a child of a child of b1, and I should get b1. And this can continue: if I start searching from a child of child of....of b1, I sohuld get b1.

 
Hi

Ok, got this part.
Code:
[b]function[/b] huh(start,container)
{
  [b]if[/b] (typeof(start)!=[i]'object'[/i]) start=document.getElementById(start)
  [b]if[/b] (typeof(container)!=[i]'object'[/i]) container=document.getElementById(container)

  [b]var[/b] temp=start
  [b]while[/b] (temp!=null && temp.parentNode!=container) temp=temp.parentNode

  [b]return[/b] temp
}
And you can pass to the function either objects or [tt]id[/tt]s :
Code:
huh([i]'current1'[/i],[i]'a'[/i])
huh(document.getElementById([i]'current2'[/i]),document.getElementById([i]'a'[/i]))
huh([i]'current3'[/i],[i]'a'[/i]) [gray]// returns null[/gray]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top