Jquery using function to return a value










2
















  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










share|improve this question
























  • What do you hope to accomplish with num1.num2? How do you set num1 and num2 when calling add(num1, num2) inside the jQuery?

    – vol7ron
    Nov 14 '18 at 2:29















2
















  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










share|improve this question
























  • What do you hope to accomplish with num1.num2? How do you set num1 and num2 when calling add(num1, num2) inside the jQuery?

    – vol7ron
    Nov 14 '18 at 2:29













2












2








2









  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










share|improve this question

















  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







javascript jquery html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 2:24









Jack Bashford

7,46931338




7,46931338










asked Nov 14 '18 at 2:19









Sudhir JangamSudhir Jangam

163




163












  • What do you hope to accomplish with num1.num2? How do you set num1 and num2 when calling add(num1, num2) inside the jQuery?

    – vol7ron
    Nov 14 '18 at 2:29

















  • What do you hope to accomplish with num1.num2? How do you set num1 and num2 when calling add(num1, num2) inside the jQuery?

    – vol7ron
    Nov 14 '18 at 2:29
















What do you hope to accomplish with num1.num2? How do you set num1 and num2 when calling add(num1, num2) inside the jQuery?

– vol7ron
Nov 14 '18 at 2:29





What do you hope to accomplish with num1.num2? How do you set num1 and num2 when calling add(num1, num2) inside the jQuery?

– vol7ron
Nov 14 '18 at 2:29












3 Answers
3






active

oldest

votes


















2














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 />








share|improve this answer

























  • Um, where are the typos in the parseInt() methods in the OP's code? It looks fine to me.

    – Jack Bashford
    Nov 14 '18 at 2:30






  • 1





    this one: parstInt(number2);

    – Shidersz
    Nov 14 '18 at 2:31











  • Oh, right. I didn't see that.

    – Jack Bashford
    Nov 14 '18 at 2:36











  • I changed the code as below, still no change var num1 = $("#num1").val(); var num2 = $("#num2").val(); $('#total').val(add(num1, num2));

    – Sudhir Jangam
    Nov 14 '18 at 2:39











  • @JackBashford I also won't note it if wasn't for the help of the console debug from the snippet tool.

    – Shidersz
    Nov 14 '18 at 2:39


















2














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.






share|improve this answer

























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

    – Sudhir Jangam
    Nov 14 '18 at 2:28











  • still the same result

    – Sudhir Jangam
    Nov 14 '18 at 2:28











  • OK, I'll edit my post with what I believe to be the correct code.

    – Jack Bashford
    Nov 14 '18 at 2:28











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

    – Sudhir Jangam
    Nov 14 '18 at 2:29











  • @SudhirJangam Fixed now, any better?

    – Jack Bashford
    Nov 14 '18 at 2:29


















1

















$('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());





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292270%2fjquery-using-function-to-return-a-value%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    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 />








    share|improve this answer

























    • Um, where are the typos in the parseInt() methods in the OP's code? It looks fine to me.

      – Jack Bashford
      Nov 14 '18 at 2:30






    • 1





      this one: parstInt(number2);

      – Shidersz
      Nov 14 '18 at 2:31











    • Oh, right. I didn't see that.

      – Jack Bashford
      Nov 14 '18 at 2:36











    • I changed the code as below, still no change var num1 = $("#num1").val(); var num2 = $("#num2").val(); $('#total').val(add(num1, num2));

      – Sudhir Jangam
      Nov 14 '18 at 2:39











    • @JackBashford I also won't note it if wasn't for the help of the console debug from the snippet tool.

      – Shidersz
      Nov 14 '18 at 2:39















    2














    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 />








    share|improve this answer

























    • Um, where are the typos in the parseInt() methods in the OP's code? It looks fine to me.

      – Jack Bashford
      Nov 14 '18 at 2:30






    • 1





      this one: parstInt(number2);

      – Shidersz
      Nov 14 '18 at 2:31











    • Oh, right. I didn't see that.

      – Jack Bashford
      Nov 14 '18 at 2:36











    • I changed the code as below, still no change var num1 = $("#num1").val(); var num2 = $("#num2").val(); $('#total').val(add(num1, num2));

      – Sudhir Jangam
      Nov 14 '18 at 2:39











    • @JackBashford I also won't note it if wasn't for the help of the console debug from the snippet tool.

      – Shidersz
      Nov 14 '18 at 2:39













    2












    2








    2







    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 />








    share|improve this answer















    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();
    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();
    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 />






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 2:32

























    answered Nov 14 '18 at 2:29









    ShiderszShidersz

    6,0692729




    6,0692729












    • Um, where are the typos in the parseInt() methods in the OP's code? It looks fine to me.

      – Jack Bashford
      Nov 14 '18 at 2:30






    • 1





      this one: parstInt(number2);

      – Shidersz
      Nov 14 '18 at 2:31











    • Oh, right. I didn't see that.

      – Jack Bashford
      Nov 14 '18 at 2:36











    • I changed the code as below, still no change var num1 = $("#num1").val(); var num2 = $("#num2").val(); $('#total').val(add(num1, num2));

      – Sudhir Jangam
      Nov 14 '18 at 2:39











    • @JackBashford I also won't note it if wasn't for the help of the console debug from the snippet tool.

      – Shidersz
      Nov 14 '18 at 2:39

















    • Um, where are the typos in the parseInt() methods in the OP's code? It looks fine to me.

      – Jack Bashford
      Nov 14 '18 at 2:30






    • 1





      this one: parstInt(number2);

      – Shidersz
      Nov 14 '18 at 2:31











    • Oh, right. I didn't see that.

      – Jack Bashford
      Nov 14 '18 at 2:36











    • I changed the code as below, still no change var num1 = $("#num1").val(); var num2 = $("#num2").val(); $('#total').val(add(num1, num2));

      – Sudhir Jangam
      Nov 14 '18 at 2:39











    • @JackBashford I also won't note it if wasn't for the help of the console debug from the snippet tool.

      – Shidersz
      Nov 14 '18 at 2:39
















    Um, where are the typos in the parseInt() methods in the OP's code? It looks fine to me.

    – Jack Bashford
    Nov 14 '18 at 2:30





    Um, where are the typos in the parseInt() methods in the OP's code? It looks fine to me.

    – Jack Bashford
    Nov 14 '18 at 2:30




    1




    1





    this one: parstInt(number2);

    – Shidersz
    Nov 14 '18 at 2:31





    this one: parstInt(number2);

    – Shidersz
    Nov 14 '18 at 2:31













    Oh, right. I didn't see that.

    – Jack Bashford
    Nov 14 '18 at 2:36





    Oh, right. I didn't see that.

    – Jack Bashford
    Nov 14 '18 at 2:36













    I changed the code as below, still no change var num1 = $("#num1").val(); var num2 = $("#num2").val(); $('#total').val(add(num1, num2));

    – Sudhir Jangam
    Nov 14 '18 at 2:39





    I changed the code as below, still no change var num1 = $("#num1").val(); var num2 = $("#num2").val(); $('#total').val(add(num1, num2));

    – Sudhir Jangam
    Nov 14 '18 at 2:39













    @JackBashford I also won't note it if wasn't for the help of the console debug from the snippet tool.

    – Shidersz
    Nov 14 '18 at 2:39





    @JackBashford I also won't note it if wasn't for the help of the console debug from the snippet tool.

    – Shidersz
    Nov 14 '18 at 2:39













    2














    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.






    share|improve this answer

























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

      – Sudhir Jangam
      Nov 14 '18 at 2:28











    • still the same result

      – Sudhir Jangam
      Nov 14 '18 at 2:28











    • OK, I'll edit my post with what I believe to be the correct code.

      – Jack Bashford
      Nov 14 '18 at 2:28











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

      – Sudhir Jangam
      Nov 14 '18 at 2:29











    • @SudhirJangam Fixed now, any better?

      – Jack Bashford
      Nov 14 '18 at 2:29















    2














    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.






    share|improve this answer

























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

      – Sudhir Jangam
      Nov 14 '18 at 2:28











    • still the same result

      – Sudhir Jangam
      Nov 14 '18 at 2:28











    • OK, I'll edit my post with what I believe to be the correct code.

      – Jack Bashford
      Nov 14 '18 at 2:28











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

      – Sudhir Jangam
      Nov 14 '18 at 2:29











    • @SudhirJangam Fixed now, any better?

      – Jack Bashford
      Nov 14 '18 at 2:29













    2












    2








    2







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 2:42









    Shidersz

    6,0692729




    6,0692729










    answered Nov 14 '18 at 2:24









    Jack BashfordJack Bashford

    7,46931338




    7,46931338












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

      – Sudhir Jangam
      Nov 14 '18 at 2:28











    • still the same result

      – Sudhir Jangam
      Nov 14 '18 at 2:28











    • OK, I'll edit my post with what I believe to be the correct code.

      – Jack Bashford
      Nov 14 '18 at 2:28











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

      – Sudhir Jangam
      Nov 14 '18 at 2:29











    • @SudhirJangam Fixed now, any better?

      – Jack Bashford
      Nov 14 '18 at 2:29

















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

      – Sudhir Jangam
      Nov 14 '18 at 2:28











    • still the same result

      – Sudhir Jangam
      Nov 14 '18 at 2:28











    • OK, I'll edit my post with what I believe to be the correct code.

      – Jack Bashford
      Nov 14 '18 at 2:28











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

      – Sudhir Jangam
      Nov 14 '18 at 2:29











    • @SudhirJangam Fixed now, any better?

      – Jack Bashford
      Nov 14 '18 at 2:29
















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

    – Sudhir Jangam
    Nov 14 '18 at 2:28





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

    – Sudhir Jangam
    Nov 14 '18 at 2:28













    still the same result

    – Sudhir Jangam
    Nov 14 '18 at 2:28





    still the same result

    – Sudhir Jangam
    Nov 14 '18 at 2:28













    OK, I'll edit my post with what I believe to be the correct code.

    – Jack Bashford
    Nov 14 '18 at 2:28





    OK, I'll edit my post with what I believe to be the correct code.

    – Jack Bashford
    Nov 14 '18 at 2:28













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

    – Sudhir Jangam
    Nov 14 '18 at 2:29





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

    – Sudhir Jangam
    Nov 14 '18 at 2:29













    @SudhirJangam Fixed now, any better?

    – Jack Bashford
    Nov 14 '18 at 2:29





    @SudhirJangam Fixed now, any better?

    – Jack Bashford
    Nov 14 '18 at 2:29











    1

















    $('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());





    share|improve this answer



























      1

















      $('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());





      share|improve this answer

























        1












        1








        1










        $('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());





        share|improve this answer
















        $('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());





        $('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 />





        $('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 />






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 3:22









        YazsidYazsid

        1555




        1555



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292270%2fjquery-using-function-to-return-a-value%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto