I need to find what the first child of an element is.
<div class="parent">
<div class="child"></div>
<img class="child" />
<div class="child"></div>
</div>
In this example the FIRST child is a div
.
<div class="parent">
<img class="child" />
<img class="child" />
<div class="child"></div>
</div>
In this example the first child is a img
.
Definition and UsageThe firstChild property returns the first child node of a node. The firstChild property returns a node object. The firstChild property is read-only. The firstChild property is the same as childNodes[0] .
The :first-child selector is used to select the specified selector, only if it is the first child of its parent.
Yet another one:
var tag = $('.parent').children().get(0).nodeName;
Or if you already have any other reference to the parent element, you can simply access its children
property (assuming it is a DOM node, not a jQuery object):
var tag = parent.children[0].nodeName;
Reference: .children()
, .get()
, Element.children
, Node.nodeName
This would be one way
$('div.parent').children(':first');
If you want to know what type of element it is
$('div.parent').children(':first')[0].nodeName;
The [0]
will get the first underlying DOM element in the wrapped set.
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