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.



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');

Image Credits: Photo by Isis França on Unsplash.

More Similar Posts

No results found.