3

The following code works. However, it shows the "newtext" and then it simply disappears.

Can someone please let me know why it is not staying?

Google Chrome is what I am using to test with.

<script language="javascript" type="text/javascript">
    function addtext() {
        var newtext = "\nThis is the new text";
        document.myforms.test1.value += newtext;
    }
</script>
<form name="myforms">
    <textarea rows="10" cols="20" id="test1">
    line one
    line two
    line three
    line four
    line five</textarea>
    <button onclick="addtext()">Click Me! to</button>
</form>
Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
Wayne Barron
  • 304
  • 4
  • 16
  • I just finally got around to looking at this. And HOW in the world is this the same as what you say it is? I looked at that other thread, and it is NOT the same. Please, explain to me how you come to the conclusion that this is a duplicate of that one? – Wayne Barron Apr 24 '17 at 15:50

1 Answers1

4

The button tag has the type=submit by default so your page will be refreshed after the click and the textarea value will be reseted to the default value, you should add type=button to avoid that :

<button type="button" onclick="addtext()">Click Me! to</button>

Hope this helps.

function addtext() {
  var newtext = "\nThis is the new text";
  document.myforms.test1.value += newtext;
}
<form name="myforms">
<textarea rows="10" cols="20" id="test1">
line one
line two
line three
line four
line five</textarea>
<button type='button' onclick="addtext()">Click Me! to</button>
</form>
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101