0

I need to check when an image is loaded. The result has to be something like this:

  $('.testimage').onload(function() {
  // do stuff
  });

All help is appreciated.

Shingala94
  • 404
  • 3
  • 18
  • possible duplicate of [Browser-independent way to detect when image has been loaded](http://stackoverflow.com/questions/821516/browser-independent-way-to-detect-when-image-has-been-loaded) – Kabie Sep 04 '13 at 10:28

3 Answers3

2

try this

 $('.testimage').load(function() {
  // do stuff
 });

if you are using jquery version 1.7+

 $('.testimage').on('load', function() {
  // do stuff
 });
bipen
  • 36,319
  • 9
  • 49
  • 62
1

There is no onload method in jQuery (although there is an underlying onload property on the raw DOM). There is a load method but it (confusingly) has different meanings (See http://api.jquery.com/load-event/ and http://api.jquery.com/load/ ) depending on the context you use it in, and the version that attaches a load event handler has been deprecated.

The standard jQuery way to attach event handlers is via the on method:

  $('.testimage').on('load', function() {
      // do stuff
  });
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

Please try this fiddle

<img src="http://***.**/***/***/book.gif" alt="Book" id="book" width="200">

$('#book').load(function () {
    alert('loaded');
});
user2368299
  • 369
  • 3
  • 14
  • Two people have already given (more or less) identical answers. One of them over 10 minutes before you gave this answer. If you're going to answer a question that has already been answered, make it a better answer. – Quentin Sep 04 '13 at 10:37
  • @Quentin my answer is better, because i add fiddle and you can see it workelly – user2368299 Sep 04 '13 at 10:39
  • Copying the code into a fiddle is worthy of a comment, not a new answer. – Quentin Sep 04 '13 at 10:41