views:

49

answers:

3

Hi if we have div like this

<div id="1">
    <input type="text" />
    <div id="2"></div>
</div>
<div id="3">
    <div id="4">
        <input type="text"  />
    </div>
</div>

now if i want to jump from div of id=4 to input tag of <div id="1"> using parent child relationship how can i jump to that particular input tag. Please help

Thanks..

A: 

document.getElementById("4").childNodes[0].

Though if you are using jquery it would be something like:

$("#4").children(":input")

spinon
+2  A: 
$('#4').parent().prev().children('input:first')

Of course this assumes that div#1 is always the previous sibling of div#3, like you have in the example.

patmortech
+1 but please take note that id's should not start with numbers... ;)
Reigel
Good point. I would not do that myself, but wanted to use his example data exactly like he had it.
patmortech
A: 

IDs can't start with a number, so I change your code to:

<div id="id1">
  <input type="text" />
  <div id="id2"></div>
</div>
<div id="id3">
  <div id="id4">
    <input type="text"  />
  </div>
</div>

So, that makes it:

$("#id4").parents("body").children("div:first").children("input")

Another, shorter version:

$("#id4").parents("body").find('input')

Or the fast way:

$("input:first")
Gert G
in HTML4 yes.. in HTML5 it can be any string as long as it's unique.
Anurag
You're right. I've not moved onto HTML 5 yet. :)
Gert G