5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

javascriptにおけるテキストエリアへの追加と削除

Last updated at Posted at 2020-12-16

#1.はじめに
まずはhtmlコードの型

<input type="text" id="text1">
<input type="button" value="追加" onclick="addText();">
<input type="button" value="削除" onclick="deleteText();">
<br>
<textarea id="area1"></textarea>

次にjavascriptのコードの型

<script>   
    function addText() {
 
    }

    function deleteText() {
        
    }

#2.プロセス

①まずは追加ボタンの入力値を代入。
 ※.valueでタイプした値を取得できる。

var ta1 = document.getElementById("text1").value;

②入力した値をテキストエリアに代入していく。
※"+="で次々に代入していける。
※"\n" で改行

document.getElementById("area1").value += ta1  + "\n";

③テキストを変数に代入して、変数の値を空白にする。

function deleteText() {
  let ta2 = document.getElementById("area1");
  ta2.value = "";
}

#3. 完成形

function addText() {
  var ta1 = document.getElementById("text1").value;
  document.getElementById("area1").value += ta1;
}

function deleteText() {
  let ta2 = document.getElementById("area1");
  ta2.value = "";
}

以上

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?