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.
codeonly and where have you placed this script?codeelement existstextareaand 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.