dom_dom0600_001

テキスト入力要素の値を取得するために使用する正しいプロパティはどれですか?

  1. innerHTML
  2. value
  3. textContent
  4. getAttribute(“text”)
正解
  1. value
解説

入力要素の値は value プロパティを用いて取得・設定します。innerHTML や textContent は通常の要素の内容を扱うため、フォーム入力値には使えません。

使用例(サンプルコード)

[ javascript ]

let input = document.querySelector("#username");
console.log(input.value);