Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the first child id inside the div using JQuery

Tags:

jquery

dom

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?

like image 921
Geeth Avatar asked Mar 08 '10 08:03

Geeth


People also ask

Is first child jQuery?

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.

How do you get children of children in jQuery?

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.

What is the use of parent () and child () method in jQuery?

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.


1 Answers

$("#id1:first-child").attr("id")
like image 198
Dustin Laine Avatar answered Oct 21 '22 13:10

Dustin Laine