vabm vabm - 1 year ago 62
HTML Question

Access ID of unknown child DIV with javascript / jquery

I am trying to access the id of an unknown div using its parent id. A simplified version of my html is something like:

<div id="firstId">
<div id="secondId"></div>

Just to be clear, I don't know the id of the child div. Then, I am trying to use the DOM to access it with:

var idd = $(".firstId").children().attr("id");

But I get an
alert (fiddle). Not sure what might be the problem.

Answer Source

You have provided the Id and accessing it by using class selector, which is wrong. Try this:

var idd = $("#firstId").children().attr("id");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download