tags:

views:

153

answers:

3

Hi,

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?

Geetha

+2  A: 
$("#id1:first-child").attr("id")
Dustin Laine
`.attr('id')` will get the id
Paul Creasey
You caught me in mid-edit. +1
Dustin Laine
@durilai: you caught me in mid-edit :P
N 1.1
+2  A: 
$('#id1 a:first-child').attr('id')
bruno
He wants the ID of the first anchor (a1). This would give him the first child of the first anchor. It would be (a2).
Dustin Laine
for some reason, your code won't work with me. I get always undefined.and the piece of code that i wrot gives me the first id (a1) and not a2. if i read this: http://api.jquery.com/first-child-selector/ I asume that my code will search at the first "<a ../>" in the div "id1" ...correct me if i'm wrong..bruno
bruno
+2  A: 
$('#id1:first-child').attr('id')
N 1.1
See my response to @bruno's comment. Same thing.
Dustin Laine