'Fill a textarea using JavaScript

I have the following code:

<div>
    <textarea id="tinyeditor" name="description_evenement"></textarea>
    <iframe width="584" height="175">
        #document
            <html>
                <head>
                    <link href="custom.css" rel="stylesheet"></link>
                </head>
                <body id="editor" contenteditable="true">
                    Hello, World!
                </body>
            </html>
    </iframe>
</div>

I need to to fill the textarea with the value "Hello, World!" using JavaScript before submitting. I used the following code, but it's not working:

<script type="text/javascript">
function replace(){
    var content = document.getElementById('editor').innerHTML;
    document.getElementById("tinyeditor").value = content;
}

</script>

How can I do it?



Solution 1:[1]

Try this..

<script type="text/javascript">
function replace(){
    var content = document.body.innerHTML;
    document.getElementById("tinyeditor").value = content;
}

</script>

Solution 2:[2]

It might be because the textarea is in the parent element and the body is in the iframe...give your iframe an id (e.g., myFrame) and try:

var content = document.getElementById('myFrame').contentWindow.document.getElementById('editor').innerHTML;

Everything else would stay the same.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jameem
Solution 2 luciole75w