简体   繁体   中英

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>

what i need to do is to fill the textarea with the value "Hello world!" using javascript before submitting, i used the folowing code but it's not working :

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

</script>

any help would be appreciated

Try this..

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

</script>

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

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

Everything else would stay the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM