I am making a sortable and resizable image album. In there, I am trying to add a feature to create the image as a link.
Image is clicked
Then wrap that image with a div which displays an edit button on top of that image.
a tag
with the given url and name.Demo at jsfiddle
This is ok. But what I would like to do is:
How can I achieve this? I think unwrap() will the trick to remove the edit div from the image but how will I know if it is the same image, or something other than that?
js snippet:
$('#sortable li img').on("click", function () {
$image = $(this);
image_resize($image);
edit_image($image);
});
function edit_image(image) {
image.wrap('<div id="edit-image"></div>');
$('#edit-image').prepend('<a href="#">EDIT</a>');
$("#edit-image a").center(true).css("cursor", "pointer").css("z-index", "1");
$('#edit-image a').on("click", function () {
alert("clicked on edit");
if (image.parent().is("a")) {
var img_link = image.parent().attr("href");
var img_name = image.parent().attr("alt");
$('#image_link_dialog #input #link').val(img_link);
$('#image_link_dialog #input #name').val(img_name);
}
$('#image_link_dialog').css("display", "block").css("z-index", "2");
$('#image_link_dialog').center(false);
$('#image_link_dialog').draggable();
});
$('#image_link_dialog .dialog_handle a').on("click", function () {
if (!$('#image_link_dialog #input #name').val() == '') {
var img_name = $('#image_link_dialog #input #name').val();
console.log('img_name: ' + img_name);
}
if (!$('#image_link_dialog #input #link').val() == '') {
var img_link = $('#image_link_dialog #input #link').val();
console.log('img_link: ' + img_link);
if (img_name) {
if (image.parent().is("a")) {
image.parent().attr("alt", img_name);
image.parent().attr("href", img_link);
} else {
image.wrap(function () {
return "<a class='img_a' href='" + img_link + "' alt='" + img_name + "'></a>";
});
}
} else {
if (image.parent().is("a")) {
image.parent().attr("href", img_link);
} else {
image.wrap(function () {
return "<a class='img_a' href='" + img_link + "'></a>";
})
}
}
} else {
alert('please enter link url');
}
$('#image_link_dialog #input #link').val('');
$('#image_link_dialog #input #name').val('');
$('#image_link_dialog').css("display", "none");
})
}
To avoid adding multiple wrappers you can do something like this:
$('li.ui-sortable-handle').click(function(){
$('li.ui-sortable-handle').removeClass('edit-image'); //remove previously selected images
$(this).addClass('edit-image');
});
And to do editing :
$('body').on('click','.edit-image', function(){
//do whatever you want. the click event is as example edit and it to any event that suits you
});
To remove edit flag if something else is clicked:
$('body').on('click','*',function(e){
if(!$(this).hasClass('.ui-sortable-handle') $('li.ui-sortable-handle').removeClass('edit-image');
e.stopPropagation();
});
of course I suggest adding another class other than ui-sortable-handle
to you li
s and use them in the above code as there might be other elements with class 'ui-sortable-handle' in your page.
I have edited your fiddle to demonstrate a possible solution: jsfiddle
To check wether a image is currently edited i add a class. This later helps me to identify the currently edited image. I added alerts to show when you have to stop the edit mode.
The following snippet allows you to catch the event of clicking anything but an image.
$(document).on("click", function (event) {
alert('stop edit for current image');
});
Adding event.stopPropagation()
to the onCLick of the images stops the $(document).on("click",...
from capturing this click as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With