0

Why won't this work to set text of text area?

document.getElementById("code").value = "hey farmer";

I even tried: document.getElementById('code').value = "hey farmer";

Is there a way to use getElementByName to set value?

Any help is appreciated

[html]

    <form>
        <textarea id="code" name="code">
            Lollllllll
</textarea>
    </form>
12
  • 2
    What does your HTML look like? Commented Sep 19, 2013 at 18:20
  • Are you sure it is a textarea and the id is code only and where have you placed this script? Commented Sep 19, 2013 at 18:20
  • 1
    Either you have more than one element with the same ID, or you're running your script before the code element exists Commented Sep 19, 2013 at 18:25
  • 1
    @fdsfdfsdfdsw It would be nice, if you could save an example of this "read-only" behaviour to jsfiddle.net . Your problem is not reproduceable with the code you've provided. Btw, are you using a JS library, like CodeMirror? Commented Sep 19, 2013 at 18:40
  • 1
    @fdsfdfsdfdsw That's it then, those elements you see on the page are not the original elements. CodeMirror hides your original textarea and shows other elements instead. To solve this we need to see, how you've created the CM object(s). I've added the CodeMirror tag to your question, it's a very important part of your problem. Commented Sep 19, 2013 at 18:48

4 Answers 4

2

It works just fine. In

<textarea id=code>Foo</textarea>
<button type=button onclick="document.getElementById('code').value = 'Bar'">

the text changes to "Bar" when the button is clicked.

HTML5 says

The value attribute must, on getting, return the element's API value; on setting, it must set the element's raw value to the new value, set the element's dirty value flag to true, and should then move the text entry cursor position to the end of the text field, unselecting any selected text and resetting the selection direction to none.

so this is speced behavior.


The problem is probably that you have multiple (or zero) elements with id="code" when your code runs so it's getting the wrong one.


Another problem may be that your <textarea> is immutable.

http://www.w3.org/TR/html5/forms.html says

A textarea element is mutable if it is neither disabled nor has a readonly attribute specified.

Sign up to request clarification or add additional context in comments.

Comments

0
 <form>
    <textarea id="code" name="code">
        Lollllllll
  </textarea>
</form>

Replace To :

<form name="yourform">
    <textarea id="code" name="code">
        Lollllllll
   </textarea>
</form>


document.forms['yourform']['code'].value = 'yourvalue';

3 Comments

Why is this better than getElementById()?
he told that document.getElementById("code").value = "hey farmer"; is not working s.
It's not working because there's something causing it to not work. While this is an alternative for fetching elements, there's nothing to say that whatever is causing getElementById to fail won't also cause your code to fail. In other words, you're not addressing the immediate problem.
0

If you paste your script after your html, it would definitley work up. Below is the working code:

http://jsbin.com/EDOJEZ/2/edit?html,output

Again, I repeat, you gotta put your script block after your html markup.

Comments

0

Native API

document.getElementById("id").value="Radha Mohan"

jQuery API

$("#couponRestrictionBean\\.includeProductIds").val("Radha")

3 Comments

And how is that better?
And why do you have $("#couponRestrictionBean\.includeProductIds")
$ is for jquery if you want to use only javascript use document.getElementById("id").value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.