I am trying to make a simple sum function. it's not working properly. What is wrong?
<head>
<meta charset="utf-8">
<title>Calculator using jQuery and Ajax</title>
<script>
$(function () {
$("button").on('click' , function(){
Add();
})
});
function Add(){
var num1 = parseInt($("#num1").val());
var num2 = parseInt($("#num2").val());
var result = num1 num2;
}
</script>
</head>
<body>
<form>
<input type="number" name="num1" id="num1">
<input type="number" name="num2" id="num2">
<button type="button" onclick="Add">=</button>
<div id = "res"></div>
</form>
</body>
the =
button does not show the answer. I don't know what is wrong!!
CodePudding user response:
You are getting the sum by var result = num1 num2;
but you are not assigning the variable to <div id="res"></div>
.
Try adding this after var result [...]
:
$("#res").html(result);
CodePudding user response:
First thing first you haven't import the JQuery library to your code. You can import by simply pasting this to your head tag.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
Second You haven't sent the result to the output. You can add this
$("#res").html(result);
below var result = num1 num2;