shirkkan
shirkkan

Reputation: 124

Make html() include content typed into textarea

I've got webpage with this structure:

<div id="report_content">
    Some information
    <textarea name="personal"></textarea>

    <b>Other information</b>
    <textarea name="work"></textarea>
</div>

After writing some text in the textareas, I use jquery to get the entire html. The result is that the textareas are empty, as if I hadn't written anything inside.

I'm guessing it's because they do not accept html, but I need to get the html including textarea's content.

The only solution I've found so far is to convert textareas to divs and then assign them the textarea content.

Is there any other way to avoid this conversion?

Upvotes: 1

Views: 123

Answers (1)

Smern
Smern

Reputation: 19066

The problem is that .html() will not get the value (which is what the content people type into the textearea will go into). You can set the innerHTML to what the value is before getting the full html, like this...

JSFiddle

$('textarea').each(function () {
    $(this).html($(this).val());
});

var html = $("#report_content").html();
console.log(html);

or... with less jquery wrapping...

var html = $("#report_content").find("textarea").each(function () {
    this.innerHTML = this.value;
}).end().html();

console.log(html);

Upvotes: 5

Related Questions