Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - Accessing nested child elements

Tags:

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?

like image 765
John Avatar asked Jan 31 '12 11:01

John


People also ask

What is a parent child selector?

The ("parent > child") selector selects all elements that are a direct child of the specified element.


1 Answers

You would just perform a .find() implicitly or explicitly:

$('#ParentDiv input');  // implicitly  $('#ParentDiv').find('input'); // explicitly 

Reference: .find()

like image 112
jAndy Avatar answered Oct 06 '22 00:10

jAndy