You do the script as the tin says. You get spaces because you have spaces and line breaks in <p> tags.
To remove text formatting, try the following: http://jsfiddle.net/z9xCm/18/
function edit() { var wdith = $("p").css('width'); var p = $("p:first"); var t = p.text().replace("\n", "").replace(/\s{2,}/g, " ").trim(); p.replaceWith("<textarea class='edit'>" + t + "</textarea>") $(".edit").css("width", wdith) } $("#replace").click(edit);
First, we remove line breaks, then remove some duplicate spaces, then trim the spaces at the beginning and end of the text.
A bit off topic, but it can also be rewritten as: http://jsfiddle.net/z9xCm/52/
$("#replace").click(function() { var p = $("p:first"); p.replaceWith($("<textarea/>", { "class": "edit", "text": p.text().replace("\n", "").replace(/\s{2,}/g, " ").trim(), "css": { "width": p.css('width') } })); });
Itβs the same here, but in a less compact and commented form.
$("#replace").click(function() { var p = $("p:first"); var t = p.text().replace("\n", "").replace(/\s{2,}/g, " ").trim(); var ta = $("<textarea/>", { "class": "edit", "text": t, "css": { "width": p.css('width') } }); p.replaceWith(ta); });
Note that the syntax $("...", {...}) for creating new elements with attributes was introduced in jquery 1.4 .