$(".photo_holder").html(''); previous = $("#itemContainer").find("li##{@edited_photo.id}").prev(); $("#itemContainer").find("li##{@edited_photo.id}").remove(); $("#itemContainer").find("#modaledit_#{@edited_photo.id}").remove(); if (previous.attr("id")){ previous.after("#{j render :template => 'mokio/photos/photo', :locals => {photo: @edited_photo} }"); } else { $("#itemContainer").prepend("#{j render :template => 'mokio/photos/photo', :locals => {photo: @edited_photo} }"); }