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!

Traverse HTML DOM up to find a specific tag or id

Status
Not open for further replies.

studio66

Technical User
Jan 22, 2011
3
CA
Hi. I would like to find if a specific selection or element is inside another node, but I would like to go from the bottom up. For example if a text node is inside a form node.

<form id="theForm">
...
other nodes
nested nodes

my selection: <p>Test me</p>
...
</form>

<for id="anotherForm">
...
doesn't matter
...
</form>

What I would like to do is to move up from node text "Test me" until I reach the tag form and than get the id of this tag (for compare...)
 
Hi

Abit generic way :
Code:
[b]function[/b] [COLOR=darkgoldenrod]isinside[/color][teal]([/teal]who[teal],[/teal]what[teal])[/teal]
[teal]{[/teal]
  [b]while[/b] [teal]([/teal]who[teal].[/teal]parentNode[teal])[/teal] [teal]{[/teal]
    [b]if[/b] [teal]([/teal]who[teal].[/teal]parentNode[teal].[/teal]nodeName[teal]==[/teal]what[teal])[/teal] [b]return[/b] [b]true[/b]
    who[teal]=[/teal]who[teal].[/teal]parentNode
  [teal]}[/teal]
  [b]return[/b] [b]false[/b]
[teal]}[/teal]
Just call it as [tt]isinside[teal]([/teal]reference_to_the_text_node[teal],[/teal][green]'FORM'[/green][teal])[/teal][/tt].


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top