I tried to do this for replacing a paragraph with a text area with the same content.
function edit() {
var wdith = $("p").css('width')
$("p:first").replaceWith("<textarea class='edit'>" + $("p:first").text() + "</textarea>")
$(".edit").css("width", wdith)
}
$("#replace").click(edit);
But it doesn’t work correctly. There are spaces before and after the text.
How do I fix it?
You script is doing as it says on the tin. You’re getting spaces because you have spaces and line breaks within your
<p>tags.To remove the text formatting, try this: http://jsfiddle.net/z9xCm/18/
First, we remove the line breaks, then removed multiple repeated spaces, then trim spaces at the beginning and end of the text.
Somewhat off topic, but that can also be rewritten as : http://jsfiddle.net/z9xCm/52/
Here’s the same thing, but in a less compact and commented form.
Note that the
$("...", {...})syntax for creating new elements with attributes was only introduced in jquery 1.4.