Home JQuery 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 html element within a div tag element using JQuery?

PDF Print E-mail

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

+/-
+/- Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
Security Please input the anti-spam code that you can read in the image.
 

Go Daddy $7.49 .com Sale!