JavaScript パスワード入力欄の値を取得するサンプル

環境
Windows 10 home 64bit
Google Chrome 106.0.5249.103

構文
const 変数名 = document.getElementById(パスワードのセレクタ名);
document.getElementById(セレクタ名).textContent =変数名.value;

使用例

<!DOCTYPE html>
<html>
<body>

<p>パスワード: <span id="cft"></span></p>
<input type="password" value="1234abc" id="passwd" maxlength="10" />
<p><input type="button" value="表示" onclick="show()" /></p>

<script>
  function show() {
    const pwd = document.getElementById("passwd");
    document.getElementById("cft").textContent =pwd.value;
  }
</script>

</body>
</html>

結果

「表示」ボタンを押すとパスワードのvalue値が「パスワード: 1234abc」が表示されます。

 

JavaScript

Posted by arkgame