How to get the id of the first html element within a div tag element using JQuery

Written by Ian Carnaghan · 23 sec read >

How to get the id of the first img html tag element within a specific div tag using JQuery? With getting the id I mean getting the attribute id’s value for the first element within the div tag.



We can obtain this by using the :first Selector from JQuery. Read more about the selector here



<div class=”custom_div”>

    <img id="1" name="image1" src="myImage1.jpg" width="200" border="1" />
    <img id="2" name="image2" src="myImage2.jpg" width="200" border="1" />
    <img id="5" name="image3" src="myImage3.jpg" width="200" border="1" />


        var id = $('.custom_div img:first').attr('id');

Last Updated On March 21, 2018
Written by Ian Carnaghan
I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment. Profile

Leave a Reply

Notify of