Passing HTML input value as a JavaScript Function Parameter

后端 未结 7 1723
别跟我提以往
别跟我提以往 2020-12-13 15:52

I am new to JavaScript, and I\'m trying to figure out how to pass user-inputted values as a parameter to a JavaScript function. Here is my code:

         


        
相关标签:
7条回答
  • 2020-12-13 16:27

    One way is by using document.getElementByID

    <body>
    <h1>Adding 'a' and 'b'</h1>
    
      a: <input type="number" name="a" id="a"><br>
      b: <input type="number" name="b" id="b"><br>
      <button onclick="add(document.getElementById('a').value,document.getElementById('b').value)">Add</button>
    
    <script>
      function add(a,b) {
        var sum = parseInt(a) + parseInt(b);
        alert(sum);
      }
    </script>
    </body>
    
    0 讨论(0)
提交回复
热议问题