Click here to Skip to main content
16,004,782 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Addition not working in jquery....i added jquery file correctly but its addition not working
HTML
<html>
<head>
<script type="text/javascript" src="jquery-1.12.1.min.js"></script>
<script type="text/javascript" >
$(document).ready(
function(){
$("#sub").click(
 
 var a=$("#txfirstno").val;
 var b=$("#txsecondno").val; 
  $("#result").val(a+b);
  
  );})

</script>
</head>
<body>
<form Name="vontact"id="main">
First NO:<input type="text" id="txfirstno"/></br>
Second No:<input type="text" id="txsecondno"/></br>
Answer:<input type="text" id="result"/></br>
<input type="button" id="sub" text="ok"/></br>
</form>
</body>
</html>


What I have tried:

i added jquery file correctly but its addition not working.
Posted
Updated 25-Feb-16 7:11am
v2

1 solution

There is curly bracket mismatch and secondly it was not parsing correctly so it was not working. Try with below code:
HTML
<html>
	<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
	<script type="text/javascript" >
		$(document).ready(function(){
			// Handling sub button click
			$("#sub").click(function(){
				var a=parseInt($("#txfirstno").val());
				var b=parseInt($("#txsecondno").val()); 
				$("#result").val(a+b);
			});

			// To allow only numbers in text boxes
			$('#txfirstno, #txsecondno').keydown(function (e) {
				if (e.shiftKey || e.ctrlKey || e.altKey) {
					e.preventDefault();
				} 
				else 
				{
					var key = e.keyCode;
					if (!((key == 8) || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105))) {
						e.preventDefault();
					}
				}
			});
		});

	</script>
	</head>
	<body>
	<form Name="vontact"id="main">
		First NO:<input type="text" id="txfirstno"/>
		Second No:<input type="text" id="txsecondno"/>
		Answer:<input type="text" id="result"/>
		<input type="button" id="sub" value="ok"/>
	</form>
	</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900