how to change innerHTML of textarea after typing in it?

萝らか妹 提交于 2020-01-05 07:37:54

问题


The following code works only if I don't first type in the textarea box. If i type in it, clicking on the div does nothing. Is there any way to fix this in javascript or whatever? Any help is appreciated.

<textarea id = "textarea">change this</textarea>
<div onclick = "change()">click here<div>

<script>
function change()
    {
        document.getElementById( 'textarea' ).innerHTML = 'new text';
    }
</script>

here is the jsfiddle http://jsfiddle.net/69n24agz/


回答1:


Change innerHTML to value,

<textarea id = "textarea">change this</textarea>
<div onclick = "change()">click here<div>

<script>
    function change()
    {
        document.getElementById( 'textarea' ).value = 'new text';
    }
</script>

a textarea has a value that can be altered, the innerHTML here just sets the initial value.



来源:https://stackoverflow.com/questions/27674257/how-to-change-innerhtml-of-textarea-after-typing-in-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!