How to get the first child id inside the div using JQuery.
Sample code:
<div id='id1'>
<a id='a1' />
<a id='a2' />
<a id='a3' />
</div>
I want to get the id a1
.
I have tried $('#id1 a:first-child').html()
to get the text. How to get the id?
It is a jQuery Selector used to select every element that is the first child of its parent. Return Value: It selects and returns the first child element of its parent.
The children() method returns all direct children of the selected element. The DOM tree: This method only traverse a single level down the DOM tree. To traverse down multiple levels (to return grandchildren or other descendants), use the find() method.
It is a jQuery Selector used to select all elements that are the direct child of its parent element. Parameter Values: parent: Using this, the parent element will be selected. child: Using this, the direct child element of the specified parent element will be selected.
$("#id1:first-child").attr("id")
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