When you click on ADD, some DIV is added to the Container. This DIV contain two divs - MENU div and EDIT div.
On focus EDIT div - I want to replace this EDIT Div with TEXTAREA and insert text from EDIT DIV to this TEXTAREA. Than type some text and on blur I want replace TEXTAREA back with DIV - and insert new edited text from TEXTAREA to this EDIT DIV.
But I have two problems :
- With targeting. Becouse text from MENU is inserting to TEXTAREA and MENU DIV disappears.
- When I use doubleclick for the first time to change DIV and TEXTAREA, next time it is happening just on simple click. Why and how to fix it ?
jsFIDDLE DEM
HTML
<div id="add" style="background:yellow; width:100px;"> add new </div>
<div id="container"> </div>
jQUERY
function handler4(){
var edited_DIV = $(this);
$(edited_DIV).live({
click: function() {
if ($(this).children('textarea').length === 0) {
var text_from_DIV = $(edited_DIV).find("#edit_div").text();
$(this).html("<textarea class='inputbox' >"+text_from_DIV+"</textarea>");
$("textarea.inputbox").focus();
$("textarea.inputbox").blur(function() {
var text_from_TEXTAREA = $(this).val();
$(edited_DIV).text(text_from_TEXTAREA);
});
}
}
});
}
$("#add").on({
click: function(e) {
var timestamp = Date.now();
var posx = Math.floor(Math.random() * 400);
var posy = Math.floor(Math.random() * 400);
$('#container').append(function() {
return $('<div class="add_to_this" id="' + timestamp + '" style="left:' + posx + 'px; top:' + posx + 'px; "><div id="edit_div"><div id="menu" style="color:red;"><b> MENU </b></div>Click here to edit</div></div>').dblclick(handler4).draggable({
containment: "#container",
scroll: false,
cursor: 'lock',
opacity: 0.55,
grid: [2, 2]
}).resizable();
});
}
});