tarnfeld
tarnfeld

Reputation: 26556

How to gain access to an element on the same level in the DOM?

<div id="dad">
     <img id="mum">
     <input>
</div>

With jQuery, how could i get access to the input element, get is value or set it for example? I can't work out how to access something on the same level.. and i dont want to be using ID's, just parent/child nodes so i can use the code for loads of dad div's

Thanks!

Upvotes: 2

Views: 9403

Answers (6)

shabunc
shabunc

Reputation: 24731

I guess you want to find siblings (node with same depth and same parent and in DOM tree)

$(el).next() - next element (sibling) for all elements in the set
$(el).nextAll - all following siblings
$(el).nextUntil - all following siblings, stop on some condition (not including first "bad match"). Besides, you have next adjacent selector (+) and next sibling selector.

The same about prev, prevAll and prevUntil. And you even have a siblings method. Check this out.

Upvotes: 0

Zed
Zed

Reputation: 57648

$(this).parent().children() ?

Upvotes: 1

Sinan
Sinan

Reputation: 11563

an addition to Zed,

$(this).parent().children('input');

if you give a name to your input field then you can easily select throughout the others,

$(this).parent().children('input[name=my_input]');

then you can give any value as:

$(this).parent().children('input[name=my_input]').val('any value');

Sinan.

Upvotes: 6

AbstractProblemFactory
AbstractProblemFactory

Reputation: 9811

If i understand question, you would like achieve input from mum leve? So try $("#mum ~ input")...

BTW, great site to search jquery function by category http://visualjquery.com/ +nice example.

Upvotes: 0

Justin Ethier
Justin Ethier

Reputation: 134167

Try this, to find the first child input element:

jQuery("div").find("input:first")

Upvotes: 0

code_burgar
code_burgar

Reputation: 12323

var myEl = $("#dad").children(":input");

Upvotes: 2

Related Questions