Jquery using function to return a value

Viewed 3154
  1. Created a JS function to add two numbers in index.js

    function add(number1, number2) {
    var num1 = parseInt(number1);
    var num2 = parstInt(number2);
    var num3 = num1.num2;
    return num3;
    }
    
  2. Created a html page in Visual Studio

<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title></title>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.js">     
</script>
<script src="index.js"> </script>
</head>
<body>
<h1> Adding two numbers </h1>
<input id="num1" placeholder="0" />
<input id="num2" placeholder="0" />
<button id="btnAdd" type="button"> Add</button>
<input id="total" placeholder="0" readonly />
<script type="text/javascript">
   $('document').ready(function () {
           $('#btnAdd').click(function () {
               $('#total').val(add(num1, num2));
       });
    });
</script>
</body>
</html>

When we click on "Add" button, total should be displayed But nothing happens with this code

3 Answers

You haven't declared num1 and num2 when you're calling add. Make your code this:

$('document').ready(function () {
    $('#btnAdd').click(function () {
        $('#total').val(add($("#num1").val(), $("#num2").val()));
    });
});

And it should work.

EDIT: Change your add function to this:

function add(number1, number2) {
    var num1 = parseInt(number1);
    var num2 = parseInt(number2);
    var num3 = num1 + num2;
    return num3;
}

The . operator doesn't add two numbers.

You have some errors on your code:

1) you have to get values from the inputs elements.

2) You have a typo error on one of the parseInt() methods.

3) To adds number you where using .? Should no be the + operator.

Check the next example with the fixes:

$('document').ready(function()
{
    $('#btnAdd').click(function ()
    {
        var num1 = $("#num1").val();
        var num2 = $("#num2").val();
        $('#total').val(add(num1, num2));
    });
});

function add(number1, number2)
{
    var num1 = parseInt(number1);
    var num2 = parseInt(number2);
    var num3 = num1 + num2;
    return num3;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h1> Adding two numbers </h1>
<input id="num1" placeholder="0" />
<input id="num2" placeholder="0" />
<button id="btnAdd" type="button"> Add</button>
<input id="total" placeholder="0" readonly />

$('document').ready(function()
{
    $('#btnAdd').click(function ()
    {
        var num1 = ( $("#num1").val()==""?0:$("#num1").val());
        var num2 = ( $("#num2").val()==""?0:$("#num2").val());
        $('#total').val(add(num1, num2));
    });
});

function add(number1, number2)
{
    var num1 = parseInt(number1);
    var num2 = parseInt(number2);
    var num3 = num1 + num2;
    return num3;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h1> Adding two numbers </h1>
<input id="num1" placeholder="0" />
<input id="num2" placeholder="0" />
<button id="btnAdd" type="button"> Add</button>
<input id="total" placeholder="0" readonly />

Maybe an exception if the input is empty is necesary for your problem

( $("#num1").val()==""?0:$("#num1").val());
Related