[HTML] JavaScriptで押されたボタンの値を取り出す

 以下にサンプルコード。

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript">
      function buttonPush(obj) {
        document.getElementById('value').innerHTML = obj.value;
      }
    </script>
  </head>

  <body>
    <input type="button" value="1" onclick="buttonPush(this);">
    <input type="button" value="2" onclick="buttonPush(this);">
    <input type="button" value="3" onclick="buttonPush(this);">
    <input type="button" value="4" onclick="buttonPush(this);">
    <input type="button" value="5" onclick="buttonPush(this);">
    <input type="button" value="6" onclick="buttonPush(this);">
    <input type="button" value="7" onclick="buttonPush(this);">
    <input type="button" value="8" onclick="buttonPush(this);">
    <input type="button" value="9" onclick="buttonPush(this);">
    <input type="button" value="0" onclick="buttonPush(this);">
    <p><span id="value"></span></p>
  </body>
</html>

コメント