Let it work through him.
You cannot make a div editable. There is no such thing as an editable div, at least not yet. Therefore, the problem is to figure out what to use for editing. The text box works great. Therefore, the idea is to somehow get the text box in which the div sits.
The question is how and where do we get the text box. There are different ways, but one of them is to dynamically create a text field on the fly:
var editableText = $("<textarea />");
and replace it with a div:
$("#myDiv").replaceWith(editableText);
The text box is now in place. But it is empty, and we just replaced the div and lost everything. Therefore, we need to somehow save the div text. One way is to copy the text / html inside the div before replacing it:
var divHtml = $("#myDiv").html();
Once we have the html from the div, we can fill the text box and safely replace the div with the text box. And set the focus inside the text box, as the user may want to start editing. Combining all the work up to this point, we get:
It functions, but nothing happens when the user clicks on the div, because we did not configure any events. Let me hook up the events. When the user clicks on any div $("div").click(..) , we create a click handler and do all of the above.
$("div").click(function() { var divHtml = $(this).html();
This is good, but we need a way to return our div when the user clicks or leaves the text field. For form elements, there is a blur event that fires when the user leaves the control. This can be used to detect when the user leaves the text box and replaces the div. This time we are making an exact turn. Save the textarea value, create a dynamic div, set it to html and replace the text box.
$(editableText).blur(function() { // Preserve the value of textarea var html = $(this).val(); // create a dynamic div var viewableText = $("<div>"); // set it html viewableText.html(html); // replace out the textarea $(this).replaceWith(viewableText); });
Everything is fine, except that this new div will no longer convert to the text area when clicked. This is a newly created div, and we will have to configure the click event again. We already have all the code, but better than repeating it twice, it's better to make a function out of it.
function divClicked() { var divHtml = $(this).html(); var editableText = $("<textarea />"); editableText.val(divHtml); $(this).replaceWith(editableText); editableText.focus();
Since the whole operation is two-way reversible, we will need to do the same for the text field. Let me convert this to a function too.
function editableTextBlurred() { var html = $(this).val(); var viewableText = $("<div>"); viewableText.html(html); $(this).replaceWith(viewableText);
Putting it all together, we have 2 functions, divClicked , editableTextBlurred and the line below starts everything:
$("div").click(divClicked);
Checkout this code at http://jsfiddle.net/GeJkU/ . This is not the best way to write editable sections by any means, but just one way to start and approach the solution step by step. Honestly, I found out just like you did by writing this long play. Depart, Adio!