0

Well, I have two textareas. When a user will type in one textarea, it will appear in the second textarea simultaneously. So my codes below:

<script>
function type () {
    var text = document.getElementById('text').value;
    var code = document.getElementById('code');
    code.innerHTML = text;
}
</script>

<textarea cols="20" rows="20" id="text" onKeyUp="type();"></textarea>
<textarea cols="20" rows="20" id="code"></textarea>

And nothing is written to the second one... Help!

user1780468
  • 117
  • 3
  • 8

4 Answers4

2

You should use the value property, rather than innerHTML, of the second textarea.

=== EDIT ===

And type is a reserved word in JavaScript and you should not use it as a function name.

lqs
  • 1,434
  • 11
  • 20
  • I removed the innerHTML, and it is not working by inserting "value" in place of innerHtml... – user1780468 Oct 29 '12 at 08:12
  • and `type` is a reserved word in JavaScript. – lqs Oct 29 '12 at 08:16
  • Ya you are right, it is working right now... By the way, I am making a simple text editor, can I remove the second textarea to put an IFrame? The preview will be the same? – user1780468 Oct 29 '12 at 08:19
0
<script>
function type () {
    var text = document.getElementById('text').value;
    var code = document.getElementById('code');
    code.value = text; // this is the change
}
</script>

use .value instead of .innerHTML

Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
  • for the Iframe, if I have understood well, I will have to use Document.write, right? Assuming the second textarea is an IFrame, the code will be like this then: `code` `code` – user1780468 Oct 29 '12 at 08:40
  • if it is an iFrame you should do it like this: `document.getElementById('externaliframe').src="javascript:void( document.writeln("Added html contetnt!") );";' read more about it here http://thetechnofreak.com/technofreak/modify-iframe-with-javascript/ – Mihai Matei Oct 29 '12 at 08:57
0

You have the issue with the function name nothing other than that.

Satish Bellapu
  • 740
  • 1
  • 5
  • 15
  • for the Iframe, if I have understood well, I will have to use Document.write, right? Assuming the second textarea is an IFrame, the code will be like this then: – user1780468 Oct 29 '12 at 08:42
0

There are some problems in your code. This works fine

    <script type="text/javascript">
function type1 () {

    var text = document.getElementById("text").value;

    var code = document.getElementById("code");
    code.value = text;
}
</script>
<body>
<textarea cols="20" rows="20" id="text" onkeyup="type1()"></textarea>
<textarea cols="20" rows="20" id="code"></textarea>
</body>
polin
  • 2,745
  • 2
  • 15
  • 20
  • Yes. Another thing you need to notice that you are using single quotation in line var text = document.getElementById('text').value. There you need to use double quotation like "text". Same in "code". – polin Oct 29 '12 at 08:42