For the input field, paragraph with id="text1"and button.
<input type="text" />
<p id="text1">Lorizzle ipsizzle dolizzle sit amizzle, consectetuer adipiscing yo mamma.</p>
<button>Copy</button>
This simple jQuery will copy the text from the paragraph, hide it and show the input with the text from the paragraph.
$("button").click(function() {
$("input").val(
$("#text1").hide().text()
).show();
});
Here is an example .
Just for fun, I wrote a small script that allows you to use functions <editable>for paragraphs. Just add a class .editableto any paragraph, and jQuery will take care of the rest. I did not expand it to allow a few changes, and I almost started writing AJAX calls that are stored in the database because I'm bored. But , as the sun shines, I thought I'd better go to the beach. Here is my code and sample.
$(".editable").each(function(i) {
var $this = $(this);
$this.attr("id", "orig-" + i);
var $edit = $("<button />")
.text("edit")
.attr("id", "update-" + i)
.click(function() {
var $input = $('<input type="text" />')
.attr("id", "edit" + i)
.val($this.text());
var $save = $('<button>Save</button>')
.click(function() {
var $new = $("<p />").text($input.val());
$input.replaceWith($new);
$(this).hide();
});
$(this).replaceWith($save);
$this.replaceWith($input);
});
$(this).after($edit)
});
SAMPLE
ID, POST , .