I work with div and jquery elements. I can create a new div and add text through a button click event. Each div created has a draggable property thanks to the jqueryui plugin. In addition, I can remove individual divs through the onclick event. But I ran into this problem: since I can add text to the div, click the button. How to edit the text of a previously created div? JSFIDDLE
Jquery
$(".remove").click(function(){ $(".draggable").remove(); }); var z = 1; $('#button').click(function (e) { $('<div />', { class: 'draggable ui-widget-content', html: '<span class="close">[X]</span><span class="text">' + $('textarea').val() + '</span>', appendTo: '.middle-side', draggable: { containment: 'parent', start: function( event, ui ) { $(this).css('z-index', ++z); } } }).addClass('placement'); $('.middle-side').parent().mousemove(function(e){ var offset = $(this).offset(); var relX = e.pageX - offset.left; var relY = e.pageY - offset.top; $('.placement').css({'top': relY + 30,'left': relX + 10, 'position': 'absolute'}); }) }); $('body').on('click', '.draggable .close', function () { $(this).closest('.draggable').remove(); });
HTML
<textarea rows="4" cols="50" placeholder="Enter Text Here!"></textarea> <br/> <input type="button" id="button" value="Add Div with Text" /> <br/> <div> <div class="middle-side empty"></div> </div>
source share