This should work for you. In general, it is much easier to give your elements a class, and then in your function ready(see briefly here). You can customize any dynamic aspects of the page.
<style type="text/css">
.Word { color:#F00;cursor:text;padding:20px; }
</style>
<script type="text/javascript">
$(function () {
$("div.Editable").each(function () {
var elem = $(this),
text = elem.text(),
words = text.split(" "),
innerHtml = "<span>" + words.join("</span> <span>") + "</span>";
elem.html(innerHtml);
});
$("div.Editable span").live("dblclick", function (evt) {
$(this)
.attr({ contentEditable: true })
.addClass("Word");
}).live("mouseout", function () {
var elem = $(this);
elem
.attr({ contentEditable: false })
.removeClass("Word");
var text = elem.text(),
words = text.split(" "),
innerHtml = "<span>" + words.join("</span> <span>") + "</span>";
elem.replaceWith($(innerHtml));
});
});
</script>
<div id="Editable34" class="Editable">
Editable Editable Editable
</div>
source
share