I’ve a situation where I need to change the src of an image using jquery. I know the ID of the div and I know the format of the file name (I need to change _over to _main) but the src of the image is the only unique identifier.
My solution was to use .html() and .replace to change the image and this worked. However it had the unwelcome side effect of breaking any .hover() and .click() events associated with the image. Is there a way to restore these events or a better way of changing the img src that doesn’t break them (bearing in mind I can only ID the image through the src)?
see $.live
it breaks because you’re replacing the elements, and thus they’re losing their click handlers. if you use
.liveinstead of.bind(or its short-forms, like.click), it will work on all future elements as well.