Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get image id using jQuery?

I have written code like this. <img id='test_img' src='../../..' />

I want to get the id of this image on image load like,

$(img).load(function() {
// Here I want to get image id i.e. test_img
});

Can you please help me?

Thanks.

like image 331
gautamlakum Avatar asked Nov 29 '22 17:11

gautamlakum


1 Answers

$(img).load(function() {
   var id = $(this).attr("id");
   //etc
});

good luck!!

edit:

   //suggested by the others (most efficient)
   var id = this.id;

   //or if you want to keep using the object
   var $img = $(this);
   var id = $img.attr("id")
like image 128
Mouhannad Avatar answered Dec 04 '22 13:12

Mouhannad