Given an input field, a paragraph with id="text1"
and a 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's a sample.
Just for fun, I've written a small script that enables <editable>
functionality for paragraphs. Just add a class of .editable
to any paragraph and jQuery takes care of the rest. I haven't extended it to allow multiple edits and I almost started writing AJAX calls that save to the database because I'm bored. But since the sun is shining I thought I'd rather go to the beach. Here's my code and a 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
You really don't need all the ID's but if you're gonna do a POST with new values, you can easily refer to the elements.