简体   繁体   中英

How to fill textarea tag using javascript

I Tried all the options below

echo '<script>document.getElementById("about").value="'. $_SESSION['tmpabout'] .'"</script>';
                    echo '<script>document.getElementById("about").innerHTML="'. $_SESSION['tmpabout'] .'"</script>';
                    echo '<script>document.getElementById("about").innerTEXT="'. $_SESSION['tmpabout'] .'"</script>';

I am getting filled text in red color, highlight and it is not filling the text area. This is the output

Using document.getElementById("about").innerHTML= [...] should be working, as shown in the snipped below.

One reason why the textarea content is not being filled might be that the JavaScript is being executed before the HTML textarea is actually being loaded / rendered in the page. To solve this, read about DOMContentLoaded event .

 document.getElementById('about').innerHTML = 'test'
 <textarea id='about'></textarea>

innerText should work, not innerTEXT

But also you must take care about $_SESSION['tmpabout'] value: if it has line breaks, it will make you javascript fail. You must deal with that.

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