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

H

How Can We Help?

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

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.

 

Answer:

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

Example:

HTML

<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" />
 
</div>

JQuery

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

About the author

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.

About Author

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.

Follow Me