Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 593
tarnfeld Avatar asked Dec 31 '25 20:12

tarnfeld


2 Answers

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.

like image 65
Sinan Avatar answered Jan 02 '26 08:01

Sinan


var myEl = $("#dad").children(":input");
like image 33
code_burgar Avatar answered Jan 02 '26 09:01

code_burgar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!