Thierry Savard Saucier
Thierry Savard Saucier

Reputation: 449

jquery reload my page on its own after a simple function, any idea why?

got a simple web site, in html. I do a simple function to move the image a user click to a different div ;

function setimage(){
 $(".moving_img").click(function(){
  $('#target').after( $(this));
 });
}

but, after having moved the image ( also added a background change to yellow just to confirm it worked), my page suddenly reloads, console wont show me any errors, and I dont know what could cause this reload ... any idea why ? theres no ajax or any server-side code or any complicated pluggin I used ( i created everything I used and I'm no pro)

Upvotes: 1

Views: 97

Answers (1)

xdazz
xdazz

Reputation: 160833

function setimage(){
 $(".moving_img").click(function(){
  $('#target').after( $(this));
  return false; // add return false to prevent this.
 });
}

Or use event.stopPropagation()

function setimage(){
 $(".moving_img").click(function(e){
  e.stopPropagation();
  $('#target').after( $(this));
 });
}

Upvotes: 2

Related Questions