hh54188
hh54188

Reputation: 15626

Using the jquery html() function

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.

Upvotes: 0

Views: 131

Answers (3)

Luke
Luke

Reputation: 8407

? 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'));

Upvotes: 3

BenM
BenM

Reputation: 53198

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/

Upvotes: 0

BonyT
BonyT

Reputation: 10940

A textarea simply displays text, not formatted HTML.

Upvotes: 0

Related Questions