Assume I have the following HTML -
<DIV id="ParentDiv"> <DIV id="SubDiv1"></DIV> <DIV id="SubDiv2"> <INPUT id="input"> </DIV> </DIV>
To access the input element using jquery, it would be simply $("#input"). What I'm trying to do is access it, assuming I only know the ID of the top level div.
Currently I have
$($($("#ParentDiv").children()[1]).children()[0])
Which does seem to work. Is there a cleaner way of writing this, or is the way I am doing it ok?
The ("parent > child") selector selects all elements that are a direct child of the specified element.
You would just perform a .find()
implicitly or explicitly:
$('#ParentDiv input'); // implicitly $('#ParentDiv').find('input'); // explicitly
Reference: .find()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With