「JavaScript」getElementByIdでテキストエリアの値を取得

2021年10月27日

書式
const 変数名=document.getElementById(IDのセレクタ).value;
document.getElementById(IDのセレクタ).textContent = 値;

Node.textContent
textContent は Node のプロパティで、ノードおよびその子孫のテキストの内容を表します。

使用例

<textarea id="infoarea" cols="30" rows="3" maxlength="20">study skill become smart</textarea>
<!--funA関数を呼び出す -->
<p><input type="button" value="表示" onclick="funA()" /></p>
<div>テキストアリアの値:<span id="show"></span></div>
<script>
  function funA() {
    // Element オブジェクトを返す
    const res = document.getElementById("infoarea").value;
    //ノードののテキストの内容
    document.getElementById("show").textContent = res;
  }
</script>

実行結果
「表示」ボタンを押すと、divのspanセレクタに「study skill become smart」が表示されます。

JavaScript

Posted by arkgame