32

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:

<body>
<h1>Adding 'a' and 'b'</h1>
<form>
  a: <input type="number" name="a" id="a"><br>
  b: <input type="number" name="b" id="a"><br>
  <button onclick="add(a,b)">Add</button>
</form>
<script>
  function add(a,b) {
    var sum = a + b;
    alert(sum);
  }
</script>
</body>
1

9 Answers 9

41

One way is by using document.getElementByID, as below -

<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, 10) + parseInt(b, 10);
      alert(sum);
    }
  </script>
</body>

Sign up to request clarification or add additional context in comments.

2 Comments

Okay, your solution is somewhat working. However, it is concatenating it as if a and b were strings. So sum is 12 instead of 3 where a=1 and b=2.
var sum = Number(a) + Number(b);
14

Firstly an elements ID should always be unique. If your element IDs aren't unique then you would always get conflicting results. Imagine in your case using two different elements with the same ID.

<form>
  a: <input type="number" name="a" id="a"><br>
  b: <input type="number" name="b" id="b"><br>
  <button onclick="add()">Add</button>
</form>

<script>
  function add() {
    var a = document.getElementById('a').value;
    var b = document.getElementById('b').value;

    var sum = parseInt(a) + parseInt(b);
    alert(sum);
  }
</script>

1 Comment

Is there no way to actually pass these items as a parameter though?
9

1 IDs are meant to be unique

2 You dont need to pass any argument as you can call them in your javascript

<form>
  a: <input type="number" name="a" id="a"><br>
  b: <input type="number" name="b" id="b"><br>
  <button onclick="add()">Add</button>
</form>
<script>
  function add() {
    var a = document.getElementById('a').value;
    var b = document.getElementById('b').value;
    var sum = a + b;
    alert(sum);
  }
</script>

Comments

2
   <form action="" onsubmit="additon()" name="form1" id="form1">
      a: <input type="number" name="a" id="a"><br>
      b: <input type="number" name="b" id="b"><br>
      <input type="submit" value="Submit" name="submit">
   </form>
  <script>
      function additon() 
      {
           var a = document.getElementById('a').value;
           var b = document.getElementById('b').value;
           var sum = parseInt(a) + parseInt(b);
           return sum;
      }
  </script>

Comments

0

You can get the values with use of ID. But ID should be Unique.

<body>
<h1>Adding 'a' and 'b'</h1>
<form>
  a: <input type="number" name="a" id="a"><br>
  b: <input type="number" name="b" id="b"><br>
  <button onclick="add()">Add</button>
</form>
<script>
  function add() {
    a = $('#a').val();
    b = $('#b').val();
    var sum = a + b;
    alert(sum);
  }
</script>
</body>

Comments

0

Use this it will work,

<body>
<h1>Adding 'a' and 'b'</h1>
<form>
  a: <input type="number" name="a" id="a"><br>
  b: <input type="number" name="b" id="a"><br>
  <button onclick="add()">Add</button>
</form>
<script>
  function add() {
    var m = document.getElementById("a").value;
    var n = document.getElementById("b").value;
    var sum = m + n;
    alert(sum);
  }
</script>
</body>

Comments

-1

do you use jquery? if then:

$('#xx').val();

or use original javascript(DOM)

document.getElementById('xx').value

or

xxxform.xx.value;

if you want to learn more, w3chool can help you a lot.

Comments

-1
<body>
  <h1>Adding 'a' and 'b'</h1>

 <input type="number"placeholder="a" id="a"><br>
 <input type="number" placeholder="b"  id="b"><br>
  <button >Add</button>
  <p class="result">result</p>

  <script>
  const a = document.querySelector('#a')
  const b = document.querySelector('#b')
  const add = document.querySelector('button')
  const result = document.querySelector('.result')

  add.addEventListener('click',function(){
    result.innerText = parseInt(a.value)+ parseInt(b.value)
  });
</script>
</body>

<body>

<div class="container" style="text-align: center; margin-top: 40px;">
     <h1 style="margin-top">Adding a and b</h1>

     <span>Enter a : </span> <input type="number"placeholder="a" id="a"><br><br>

    <span> Enter b : </span> <input type="number" placeholder="b"  id="b"><br><br>

      <button style="margin-top: 20px;">Add</button>

      <p class="result">result</p>

</div>



  <script>
  const a = document.querySelector('#a')
  const b = document.querySelector('#b')
  const add = document.querySelector('button')
  const result = document.querySelector('.result')

  add.addEventListener('click',function(){
    result.innerText = parseInt(a.value)+ parseInt(b.value)
  });
</script>
</body>

2 Comments

Thank you for contributing to the Stack Overflow community. While this code may answer the question, might you please edit your post to add an explanation as to why or how it works as suggested by How do I write a good answer? Thanks!
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1

I like how most answers here don't know that javascript accepts every input as string unless we use parseInt. Yes, you can pass parameters in that way. Just a little modification

Var sum = parseFloat(a)+parseFloat (b); 

1 Comment

That has syntax errors in it, and a and b aren't strings (or numbers) so it doesn't make sense to pass them to parseFloat

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.