最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Changing value of textarea - Stack Overflow

programmeradmin5浏览0评论

I write this script to change the value of a textarea but I fail to do so. What's wrong with my code?

<html>
    <head>
        <script type="text/javascript">
            document.getElementsByName("status").innerHTML = "hi";
            document.getElementsByName("status").title= "hi";
            document.getElementsByName("status").placeholder= "hi";
        </script>
    </head>
    <body>
        <textarea placeholder="What's on your mind?" onfocus="window.UIComposer &amp;&amp; UIComposer.focusInstance(&quot;c4d981e9a2c98b0483252333&quot;);" name="status" id="c4d981e9a2c98b0483252333_input" title="What's on your mind?" class="DOMControl_placeholder UIComposer_TextArea">What's on your mind?</textarea>
    </body>
</html>

I write this script to change the value of a textarea but I fail to do so. What's wrong with my code?

<html>
    <head>
        <script type="text/javascript">
            document.getElementsByName("status").innerHTML = "hi";
            document.getElementsByName("status").title= "hi";
            document.getElementsByName("status").placeholder= "hi";
        </script>
    </head>
    <body>
        <textarea placeholder="What's on your mind?" onfocus="window.UIComposer &amp;&amp; UIComposer.focusInstance(&quot;c4d981e9a2c98b0483252333&quot;);" name="status" id="c4d981e9a2c98b0483252333_input" title="What's on your mind?" class="DOMControl_placeholder UIComposer_TextArea">What's on your mind?</textarea>
    </body>
</html>
Share Improve this question edited Mar 2, 2016 at 9:38 rrk 15.8k4 gold badges30 silver badges47 bronze badges asked Apr 3, 2011 at 7:27 benonebenone 6861 gold badge7 silver badges21 bronze badges 1
  • Look at my answer. its now working, just copy and try it. – Farzin Zaker Commented Apr 3, 2011 at 7:58
Add a ment  | 

5 Answers 5

Reset to default 5

Iif you try using:

var textarea = document.getElementById('c4d981e9a2c98b0483252333_input');
textarea.value = 'hi';

It should work.

Otherwise, because of the way getElementsByName works, you'd need to provide an index (zero-based) to the call to identify which of the textareas you want to work with:

var textarea = document.getElementByName('status')[0]; // selects the first element of name 'status'
textarea.value = 'hi';

Two problems

First, document.getElementsByName returns a NodeList (which is like an array), not a single element.

Second, you do nothing to delay the execution of the JS until the element actually exists. So it won't find it anyway.

  1. Change to document.getElementsByName("status")[0]
  2. Move the <script> element so it appears after the textarea.

I wouldn't be fortable with using innerHTML to modify a form control either. I'd switch to value instead.

<script type="text/javascript">
   document.getElementsByName("status")[0].value = "hi";
</script>

put this script in the body

<script type="text/javascript">
    document.getElementById("status").value= "hi";
</script>

change getElementsByName to getElementById

getElementsByName -> returns array of elements getElementById -> returns single control..

then put the script in between body tags not in header.....

<html>
<head>
</head>
<body>

<textarea placeholder="What's on your mind?" onfocus="window.UIComposer &amp;&amp; UIComposer.focusInstance(&quot;c4d981e9a2c98b0483252333&quot;);" name="status" id="c4d981e9a2c98b0483252333_input" title="What's on your mind?" class="DOMControl_placeholder UIComposer_TextArea">What's on your mind?</textarea>


<script type="text/javascript">
document.getElementById("c4d981e9a2c98b0483252333_input").value = "hi";
document.getElementsByName("status")[0].value = "hi";
</script>
</body>
</html>
发布评论

评论列表(0)

  1. 暂无评论