0

I have a paragraph

<p>hello<br>wor&nbsp;l&nbsp;d</p>

then I want show the paragraph's html in another textarea use jquery html() method

    $(function () {
        var temp = $('p').html();

        $("textarea").html(temp);
    });

but the result is

hello<br>wor l d

Why does <br> not work? I didn't change any html.

Here is the example.

2
  • Are you after displaying the html source from the paragraph's contents, or showing it with &nbsp; and <br> rendered as space and line break? Commented Jul 14, 2011 at 10:39
  • @Tomas Lycken:yes,I still want the line break,cuz I want the the mark write to the sql,so when next time I want read from the sql,it would keep as it original look like Commented Jul 14, 2011 at 11:33

3 Answers 3

3

? isn´t that clear? you are requesting the html and this one is pasted into the textbox? You will have to replace the br into linebrakes, like "\n"

var temp = $('p').html();

 $("#tt").html(temp.replace(/<br\s*(\/|)>/gi, '\n'));
Sign up to request clarification or add additional context in comments.

3 Comments

Use temp.replace(/<br\s*(\/|)>/gi, '\n') to catch 'em all.
thanks DarthJDG, but from now on, we should also say (since it begins to look not like a "fast" answer anymore) that this method replaces only once, right?
With the regex's gi flags set, it replaces all occurrences and it's doing a case-insensitive search.
0

A textarea simply displays text, not formatted HTML.

Comments

0

Yes, if you want to output it as displayed, the following code should help:

$(function() {
    var temp = $('p').html();
    var textareaval = temp.replace("<br>", "\n");
    textareaval = textareaval.replace("<br />", "\n");
    $('textarea').html(textareaval);
});

See the jsFiddle here: http://jsfiddle.net/6adgA/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.