1 Replies - 491 Views - Last Post: 10 January 2013 - 04:43 PM

#1 James1992  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 130
  • Joined: 30-October 12

How to change contents of textarea with javascript?

Posted 10 January 2013 - 04:32 PM

This is my code:

document.getElementById("1").textContent = 

for(var i=0;i<cib006.length;i++){
 		document.write(cib006[i]+"<br>"));
 		}



What I was hoping this javascript code would do, is paste the entire contents of the cib006 array into the textarea. However, this does not work. How would I change my code so that it pastes the contents?

If I were to remove the document.getelement part, then it works fine (though this would wipe the page of its contents and just display the array. Any solutions? :)

Is This A Good Question/Topic? 0
  • +

Replies To: How to change contents of textarea with javascript?

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3720
  • View blog
  • Posts: 12,946
  • Joined: 12-December 12

Re: How to change contents of textarea with javascript?

Posted 10 January 2013 - 04:43 PM

Note: IDs should not just be a number, and should not (preferably) start with a number.

Anyway, if you are writing to a textarea then you should set its value. It's best to assemble a string first and you need to use line-breaks \n rather than (HTML) br's.

DON'T USE DOCUMENT WRITE! If you are (as you should be) running this code after the page has loaded, then document.write() will wipe-out the page.

var theTA = document.getElementById("1"); // change this from 1!
var taText = '', cib006L = cib006.length;
for (var i=0; i < cib006L; i++) {
    taText += cib006[i] + '\n';
}
theTA.value = taText;


Your original code is incorrect: You can't assign a for-loop to textContent (or even innerText)?!

This post has been edited by andrewsw: 10 January 2013 - 04:48 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1