I want to make a <p>
tag editable by clicking it. My problem: It can only be edited once. After a second click, I get an error message in my console.
See the fiddle here: http://jsfiddle.net/LnD8d/1/
HTML:
<div id="profile_description"><p>Click to add description</p></div>
JAVASCRIPT:
function editDiv(element_to_be_edited, update_description) {
var divHtml = element_to_be_edited.text();
var editableText = $("<textarea />");
editableText.val(divHtml);
element_to_be_edited.replaceWith(editableText);
editableText.focus();
// setup the blur event for this new textarea
editableText.on('blur', function() {
var html = $(this).val();
var viewableText = $("<p>");
viewableText.html(html);
$(this).replaceWith(viewableText);
// setup the click event for this new div
viewableText.click(editDiv);
// if update_description=true, update description ...
if (update_description == true) {
console.log('description has been updated');
}
});
} // /function editDiv();
$(document).ready(function() {
$('#profile_description p').on('click', function() {
console.log('click on p tag');
var element_to_be_edited = $(this);
var update_description = true;
editDiv(element_to_be_edited, update_description);
});
});
Can anyone see the reason why the tag can only be edited once and not on a second click?