Friday, June 26, 2009

[jQuery] Re: Multiplied load-Events on image-src-change

To be expected. Just as .click will trigger every time you click, the
.load will trigger every time the image is loaded.

And each time you click, you're telling it to add an additional load event.

Either
a) unbind("load") before the .load (useful if you want to do something
different each time), or
b) set the .load function in the $(document).ready; it should fire every
time the image is loaded from then on

Liam

CrazyFish wrote:
> Hello,
>
> I want to the change an image and do some action after loading.
>
> Following snipet is used:
>
> js:
>
> $(document).ready( function()
> {
> $("#clickableElement").click(function()
> {
> $('#imageID').attr("src", 'http://IMAGEURL/img.jpg').load
> (function()
> {
> alert(1);
> });
> });
> }
>
> html:
> ...
> <div id="clickableElement>Click me</div>
> <img src="currentImage" alt="Image" />
>
> If I click on the "clickableElement" a new image is loaded as expected
> and after that a messagebox with "1" appears.
>
> Now I click a second time and I will get 2 messageboxes. The third
> time 3 and so on..
>
> What's wrong, why does this happen?
>
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 8.5.375 / Virus Database: 270.12.92/2203 - Release Date: 06/26/09 05:53:00
>
>

No comments: