0
addition of two numbers using jquery is not working. Anyone who can figure out the issue?
<!DOCTYPE html> <html> <head> <title> addition </title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> $(document).ready(function(){ var num1= $(#v1).val(); var num1= $(#v2).val(); var sumIT= parseInt(num1)+parseInt(num2); $(":button").click(function(){ $("#sum").val(sumIT); }); }); </script> </head> <body> NUMBER 1 : <input type="text" name="NUM1" id="v1"><br> <br> NUMBER 2 : <input type="text" name="NUM2" id="v2"><br> <br> <input type="button" value="Click To Add Both Numbers" /></br> SUM : <input type="text" name="SUM12" id="sum"><br> <br> </body> </html>
3 Respostas
+ 1
I found the following issues:
1. You should get the 2 numbers and perform the sum inside the button click callback function.
You are getting the numbers and doing the sum as soon as the page loads. At this point the user did not insert the values yet.
2. you are calling the 2 numbers 'num1'. I believe what you intend is to call them 'num1' and 'num2'.
3. use quotes in:
var num1 = $("#v1").val();
var num2 = $("#v2").val();
4. give the button an id of 'button' and get it by that id.
Hope this helps
+ 3
Hi Candy Singh.
First of all, when asking questions about your code, please post the link of your code instead of plain text. That way, we can easily see the problems.
Here's a working code. Please compare every part of this code to your code to see which parts you are doing wrong.
https://code.sololearn.com/WickAW4YvkaF/?ref=app
+ 1
thank you guys