Francesca
Francesca

Reputation: 28148

innerHTML on keypress - cannot get content

I have a function which should grab the innerHTML of a textarea when the enter key is pressed.

The function seems to work with the key press, however the following line returns nothing:

var currentHTML = document.getElementById("postcontent").innerHTML;

I can't work out what's wrong with the line as it remains empty.

I've created a JS fiddle. Type something in the box and press enter, it should give you some console messages. You'll see that currentHTML remains empty when it should be grabbing the content.

http://jsfiddle.net/w4L7c2qh/1/

Upvotes: 0

Views: 217

Answers (2)

Dileep
Dileep

Reputation: 5440

Inner HTML is used to get the content from an HTML elements.

An element in HTML represents some kind of structure or semantics and generally consists of a start tag, content, and an end tag.

For Eq;

<div>
    This content can be retrieved using innerHTML.
</div>

If you need the content inside the textarea then you can use the .value property of javascript, which will get you the text inside the textarea.

document.getElementById("postcontent").value

Or Else

You can use JQuery

$("#postcontent").val();

Upvotes: 1

Werner
Werner

Reputation: 2154

Use value instead of innerHTML for textarea.

Updated you demo: fiddle

Upvotes: 1

Related Questions