Get textarea text with JavaScript or jQuery

JavascriptJqueryHtmlTextarea

Javascript Problem Overview


I have an iframe that contains a textarea, like so:

<html>
<body>

<form id="form1">
<div>
    <textarea id="area1" rows="15"></textarea>
</div>
</form>

</body>
</html>

I want to get the textarea text in the parent page. I've tried this:

var text = $('#frame1').contents().find('#area1').val();

But an empty string is returned. However, if I put a value within <textarea> tags this value is returned successfully:

<textarea id="area1" rows="15">something</textarea>

How can I get the value of the textarea from the page which contains the iframe?

Javascript Solutions


Solution 1 - Javascript

Reading the <textarea>'s content:
var text1 = document.getElementById('myTextArea').value;     // plain JavaScript
var text2 = $("#myTextArea").val();                          // jQuery
Writing to the <textarea>:
document.getElementById('myTextArea').value = 'new value';   // plain JavaScript
$("#myTextArea").val('new value');                           // jQuery

See demo JSFiddle here.

Do not use .html() or .innerHTML!

jQuery's .html() and JavaScript's .innerHTML should not be used, as they do not pick up changes to the textarea's text.

When the user types in the textarea, the .html() won’t return the typed value, but the original one -- check the demo fiddle above for an example.

Solution 2 - Javascript

To get the value from a textarea with an id you just have to do

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

If you are having more than one element with the same id in the document then the HTML is invalid.

Solution 3 - Javascript

You could use val().

var value = $('#area1').val();
$('#VAL_DISPLAY').html(value);

Solution 4 - Javascript

Get textarea text with JavaScript:

<!DOCTYPE html>
<body>
<form id="form1">
    <div>
        <textarea id="area1" rows="5">Yes</textarea>
        <input type="button" value="get txt" onclick="go()" />
        <br />
        <p id="as">Now what</p>
    </div>
</form>
</body>

function go() {
    var c1 = document.getElementById('area1').value;
    var d1 = document.getElementById('as');
    d1.innerHTML = c1;
}

http://jsfiddle.net/PUpeJ/3/

Solution 5 - Javascript

Try this:

var info = document.getElementById("area1").value; // JavaScript
var info = $("#area1").val(); // jQuery

Solution 6 - Javascript

Try .html() instead of .val():

var text = $('#frame1').contents().find('#area1').html();

Solution 7 - Javascript

As Darin Dimitrov said, if it is not an iframe on the same domain, it is not possible. If it is, check that $("#frame1").contents() returns all it should, and then check if the textbox is found:

$("#frame1").contents().find("#area1").length should be 1.

If when your textarea is "empty", an empty string is returned and when it has some text entered that text is returned, then it is working perfect!! When the textarea is empty, an empty string is returned!

Here there is one way. It is not very pretty, but it works:

Outside the iframe you will access the textarea like this:

window.textAreaInIframe

And inside the iframe (which I assume has jQuery) in the document ready, add this code:

$("#area1").change(function() {
    window.parent.textAreaInIframe = $(this).val();
}).trigger("change");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionantoniView Question on Stackoverflow
Solution 1 - JavascriptacdcjuniorView Answer on Stackoverflow
Solution 2 - JavascriptrahulView Answer on Stackoverflow
Solution 3 - JavascriptShawn31313View Answer on Stackoverflow
Solution 4 - JavascriptdearnotmineView Answer on Stackoverflow
Solution 5 - JavascriptKingsley LiuView Answer on Stackoverflow
Solution 6 - JavascriptTimon. ZView Answer on Stackoverflow
Solution 7 - JavascriptDiegoView Answer on Stackoverflow