Calculate values of textboxes automatically using jQuery - javascript

I want to calculate the value of the 1st 2 text boxes without a command button
Example i have 3 text boxes.
The first 2, where the numbers will be inputed and the last 1 will be the sum or product and so on.
Now i want it to auto compute.
For example i have inputed values 2 and 3 on the 1st 2 text boxes then automatically the sum or product or whatever result will be displayed on the 3rd text box.
How am i able to do this? Thanks
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$('#texttwo').keyup(function(){
var textone;
var texttwo;
textone = parseFloat($('#textone').val());
texttwo = parseFloat($('#texttwo').val());
var result = textone + texttwo;
$('#result').val(result.toFixed(2));
});
</script>
</head>
<body>
<input type="text" name="value1" id="textone">
<input type="text" name="value2" id="texttwo">
<input type="text" name="result" id="result">
</body>
</head>

You can achieve this by using jQuery.
Include jQuery in your project by putting this in your <head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Then, at the end of the file:
<script>
$('#texttwo').keyup(function(){
var textone;
var texttwo;
textone = parseFloat($('#textone').val());
texttwo = parseFloat($('#texttwo').val());
var result = textone + texttwo;
$('#result').val(result.toFixed(2));
});
</script>
This will give you the result when ever you change the value of the second box.
You will need to do this, too:
Change
<input type=text name=value1>
<input type=text name=value2>
<input type=text name=result>
to:
<input type="text" name="value1" id="textone">
<input type="text" name="value2" id="texttwo">
<input type="text" name="result" id="result">
EDIT
This is my entire file:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" name="value1" id="textone">
<input type="text" name="value2" id="texttwo">
<input type="text" name="result" id="result">
<script>
$('#texttwo').keyup(function(){
var textone;
var texttwo;
textone = parseFloat($('#textone').val());
texttwo = parseFloat($('#texttwo').val());
var result = textone + texttwo;
$('#result').val(result.toFixed(2));
});
</script>

Related

My results of calculation appear on a different page

I need to create code which automatticaly calculates the circumference when the length and width are entered in two different boxes.
I managed to do so, but I want the answer to appear underneath the "calculate" button, but the answer will make the button and boxes disappear. Could someone tell me what I need to change?
Here is my code:
<!DOCTYPE html>
<html>
<body>
<form>
Width:<br>
<input id="Width" type="text" name="Width">
<br>Length<br>
<input id="Length" type="text" name="Length">
<br><br>
<input type="button" value="Calculate circumference" onclick="doMath()">
</form>
<script>
function doMath() {
var Width1 = document.getElementById('Width').value;
var Length1 = document.getElementById('Length').value;
var Circ = parseInt(Width1) + parseInt(Width1) + parseInt(Length1) + parseInt(Length1);
document.write(Circ);
}
</script>
</body>
</html>
Do not use document.write function. The write() method is mostly used for testing: If it is used after an HTML document is fully loaded, it will delete all existing HTML. Create a paragraph tag and give an id and place your result in that paragraph.
<!DOCTYPE html>
<html>
<body>
<form>
Width:<br>
<input id="Width" type="text" name="Width">
<br>Length<br>
<input id="Length" type="text" name="Length">
<br><br>
<input type="button" value="Calculate circumference" onclick="doMath()">
<p id="result">
</p>
</form>
<script>
function doMath() {
var Width1 = document.getElementById('Width').value;
var Length1 = document.getElementById('Length').value;
var Circ = parseInt(Width1) + parseInt(Width1) + parseInt(Length1) + parseInt(Length1);
//document.write(Circ);
document.getElementById("result").textContent = Circ;
}
</script>
</body>
</html>
Try adding a black paragraph to the html and then use document.getElementById("").innerHTML to change the text inside of it.

Form with input and output immediately reload the page after printing the output

I need to do a simple homework where you enter two integers in two text fields of a form and then you compute the sum and you print it in a "text field (not editable)". My program seems to work but it prints the right output and immediately reload the page. I want the page to remain with the printed output if the user does not click again on "submit" button
Here is my code HTML & JS :
function updateExpr() {
var x1 = document.getElementById("n1").value;
var x2 = document.getElementById("n2").value;
var sum = +x1 + +x2;
document.getElementById("sum").innerHTML = +x1 + +x2;
}
<!DOCTYPE html>
<html>
<head>
<title>Number in a form</title>
<link href="mystyle.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="function.js">
</script>
</head>
<body>
<div id="mainDiv">
<H3>Insert two positive numbers</H3>
<form>
First number:<br>
<input id="n1" type="text" name="firstname"><br>
Second number:<br>
<input id="n2" type="text" name="lastname">
<BR>
<input type="submit" value="Submit" onClick="updateExpr()"/><BR><BR>
</form>
The sum is:<br>
<output id="sum" name="x" for="a b"></output>
</div>
<noscript>
Sorry: Your browser does not support or has disabled javascript
</noscript>
</body>
</html>
When form is submited, the page will be reloaded. To prevent this you should change input type attribute from submit to button.
<input type="submit" value="Submit" onClick="updateExpr()"/>
to
<input type="button" value="Submit" onClick="updateExpr()"/>
You can simply avoid server call by changing your form tag
<form onSubmit="return false">
You dont really need a form to do something like this.
Forms send values to the backend, e.g. a server.
You only want to manipulate some front-end elements like a container to have some new text inside it.
a <form> tag typically sends you to some URI with some aprameters,
this is done by the actions attribute.
<form action="addnames.php">
for example would call the addnames.php script on your server...
You dont need a server tho..look below:
function updateExpr() {
var x1 = document.getElementById("n1").value;
var x2 = document.getElementById("n2").value;
var sum =x1 +x2;
document.getElementById("sum").innerHTML = sum;
}
<h2>HTML Forms</h2>
First name:<br>
<input id="n1" type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input id="n2" type="text" name="lastname" value="Mouse">
<br><br>
<button type="submit" onclick="updateExpr()">Submit</button>
<div id="sum">
</div>
<script>
</script>
I would recommand you to add onSubmit method to the form instead of onclick to the input.
Also you better add a return : false to your function and add onsubmit="return updateExpr()".
function updateExpr() {
var x1 = document.getElementById("n1").value;
var x2 = document.getElementById("n2").value;
var sum = +x1 + +x2;
document.getElementById("sum").innerHTML = +x1 + +x2;
return false;
}
<!DOCTYPE html>
<html>
<head>
<title>Number in a form</title>
<link href="mystyle.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="function.js">
</script>
</head>
<body>
<div id="mainDiv">
<H3>Insert two positive numbers</H3>
<form onsubmit="return updateExpr()">
First number:<br>
<input id="n1" type="text" name="firstname"><br>
Second number:<br>
<input id="n2" type="text" name="lastname">
<BR>
<input type="submit" value="Submit" onClick="updateExpr()"/><BR><BR>
</form>
The sum is:<br>
<output id="sum" name="x" for="a b"></output>
</div>
<noscript>
Sorry: Your browser does not support or has disabled javascript
</noscript>
</body>
Another way of doing this would have been to add a button outside of the form linked to the function by onclick event
juste add preventDefault(), like this
function updateExpr(e) {
e.preventDefault();
var x1 = document.getElementById("n1").value;
var x2 = document.getElementById("n2").value;
var sum = +x1 + +x2;
document.getElementById("sum").innerHTML = +x1 + +x2;
}

How to Add Two Input Values And Show In The Third Input Box ?? with simple JavaScript like this help me where i was wrong [duplicate]

This question already has answers here:
Add two textbox values and display the sum in a third textbox automatically
(8 answers)
Closed 3 years ago.
1)I want to add the values of two input boxes and show it in the third input. It shows an error
2)Error: Uncaught TypeError: Cannot read property 'value' of null
at sum (js.php:21)
at HTMLButtonElement.onclick (js.php:12)
<!DOCTYPE html>
<html>
<head>
<title>JavaScript</title>
<script type="text/javascript">
function sum() {
var val1 = document.getElementById('val1').value;
var val2 = document.getElementById('val2').value;
var sum = val1 + val1;
document.getElementById('total').value = sum;
}
</script>
</head>
<body>
<input type="text" name="val1"><br><br>
<input type="text" name="val2"><br><br>
<input disabled name="total" value=""><br><br>
<button type="button" onclick="sum()" name="btn">Add</button>
</body>
</html>
I expected the sum of two inputs but it shows nothing.
You should have the attribute id for the controls which you are referencing in your code with getElementById(). You also have to convert the string value to number to perform the arithmetic operation:
function sum(){
var val1 = document.getElementById('val1').value;
var val2 = document.getElementById('val2').value;
var sum = Number(val1) + Number(val2);
document.getElementById('total').value = sum;
}
<input type="text" name="val1" id="val1"><br><br>
<input type="text" name="val2" id="val2"><br><br>
<input name="total" id="total" value=""><br><br>
<button type="button" onclick="sum()" name="btn">Add</button>
You are using HTML DOM getElementById() Method, so you should assign an Id to your input total in order to resolve the issue:
<input type="hidden" name="total" id="total" value="">
You should add Id to your input. You use getElementById, but your input doesn't have any id, so it will retourn null.
<input type="text" name="val1" id="val1"><br><br>
<input type="text" name="val2" id="id="val2"><br><br>
<input type="hidden" name="total" value="" id="total"><br><br>
You can do with click on button to sum of two input value OR with use of onblur events also
<script type="text/javascript">
function sum(){
var val_1 = document.getElementById('val1').value;
var val_2 = document.getElementById('val2').value;
var sum = Number(val_1) + Number(val_2);
document.getElementById('total').value = sum;
}
</script>
<input type="text" id="val1" onblur="sum()"><br><br>
<input type="text" id="val2" onblur="sum()"><br><br>
<input id="total" value=""><br><br>
<button type="button" onclick="sum()" name="btn">Add</button>
You should use id attribute to get the input value which you are referencing in your code with getElementById().
and You also have to convert the string value to number to perform the arithmetic operation because when we get value from inputs it will a string value
for convert string to number you can use + sign like in code
<!DOCTYPE html>
<html>
<head>
<title>JavaScript</title>
<script type="text/javascript">
function sum(){
var val1 = +document.getElementById('val1').value;
var val2 = +document.getElementById('val2').value;
document.getElementById('total').value = val1+val2;
}
</script>
</head>
<body>
<input type="text" name="val1" id="val1"><br><br>
<input type="text" name="val2" id="val2"><br><br>
<input type="hidden" name="total" id="total" value=""><br><br>
<button type="button" onclick="sum()" name="btn">Add</button>
</body>
</html>
document.getElementsByName returns a NodeList of elements so it doesn't have property .value.
This is getElementsByName snippet.
<!DOCTYPE html>
<html>
<head>
<title>JavaScript</title>
<script type="text/javascript">
function sum() {
var val1 = document.getElementsByName('val1')[0].value;
var val2 = document.getElementsByName('val2')[0].value;
var sum = parseInt(val1) + parseInt(val2); // by default it appends a string. Hence, parseInt to parse string to integer
document.getElementsByName('total')[0].value = sum;
console.log(document.getElementsByName('total')[0].value);
}
</script>
</head>
<body>
<input type="text" name="val1"><br><br>
<input type="text" name="val2"><br><br>
<input type="hidden" name="total" value=""><br><br>
<button type="button" onclick="sum()" name="btn">Add</button>
</body>
</html>
This is getElementById snippet
<!DOCTYPE html>
<html>
<head>
<title>JavaScript</title>
<script type="text/javascript">
function sum() {
var val1 = document.getElementById('val1').value;
var val2 = document.getElementById('val2').value;
var sum = parseInt(val1) + parseInt(val1); // by default it appends a string. Hence, parseInt to parse string to integer
document.getElementById('total').value = sum;
console.log(document.getElementById('total').value);
}
</script>
</head>
<body>
<input type="text" name="val1" id="val1"><br><br>
<input type="text" name="val2" id="val2"><br><br>
<input type="hidden" name="total" value="" id="total"><br><br>
<button type="button" onclick="sum()" name="btn">Add</button>
</body>
</html>
Try This
<script>
function sum(){
var val1 = document.getElementById('val1').value;
var val2 = document.getElementById('val2').value;
var sum = +val1 + +val2;
document.getElementById('total').value = sum;
}
</script>
<body>
<input type="text" name="val1" id="val1"><br><br>
<input type="text" name="val2" id="val2"><br><br>
<input type="hidden" name="total" id="total" value=""><br><br>
<button type="button" onclick="sum()" name="btn">Add</button>
</body>
OR Fetch through name
<script>
function sum(){
var val1 = document.getElementsByName('val1')[0].value;
var val2 = document.getElementsByName('val2')[0].value;
var sum = +val1 + +val2;
document.getElementsByName('total')[0].value = sum;
}
</script>
<body>
<input type="text" name="val1" ><br><br>
<input type="text" name="val2" ><br><br>
<input type="hidden" name="total" value=""><br><br>
<button type="button" onclick="sum()" name="btn">Add</button>
</body>

Pure Js (3 input calculated in real time)?

Build a basic calculator.
<html>
<body>
<input class="numb1" type=number name="secondBox" value="">
<input class="numb2" type=number name="secondBox" value="">
<input class="numb3" type=number name="thirdBox" value="" readonly>
</body>
</html>
<html><input type='number' id='one' placeholder='00' onblur='calculate()'>
/
<input type='number' id='two' placeholder='00' oninput='calculate()'>
<input type='number' id='result' placeholder='Result' disabled>
<script>
function calculate(){
var one = document.querySelector('#one').value;
var two = document.querySelector('#two').value;
var result = document.querySelector('#result');
result.value = Math.floor(one/two);
}
</script>
</html>
Here is a working example:
var evN1 = document.getElementById("no1Box");
evN1.addEventListener("input", calculate, false);
var evN2 = document.getElementById("no2Box");
evN2.addEventListener("input", calculate, false);
function calculate() {
var n1 = document.getElementById("no1Box").value;
var n2 = document.getElementById("no2Box").value;
var r = n1/n2;
document.getElementById("result").innerHTML = "Result: " + r;
}
body {padding:0;margin:0;font-family:Arial,Helvetica,sans-serif;}
div {margin:15px;}
label {display:block;}
input {border:solid 1px #888;font-size:20px;border-radius:5px;padding:2px;outline:none;}
.myInput {box-shadow:0 0 4px green;}
<!DOCTYPE html>
<html>
<body>
<label for="input">Input 1</label>
<input class="myInput" id="no1Box" type=number name="firstBox" value="">
<label for="input">Input 2</label>
<input class="myInput" id="no2Box" type=number name="secondBox" value="">
<p id="result"/>
</body>
</html>
Through document.getElementById("no1Box"); you take the element with the uniquie id that you set on html see more about getElementById here and by using .value you get the value that you introduced in your input.
After taking the element var evN2 = document.getElementById("no2Box"); add a listener to him evN2.addEventListener("input", calculate, false); and tell what function should execute when the input is changed. more examples for eventListener here
If you have questions, feel free to ask.

calculate and add a value of Form Input with javascript

i m new to javascript, just want to create simple addition in my page, below is my code,
if INPUT1 is 100 then output should be 250 but below is not working(showing output as 100150).
<form name="TEST">
<input type="number" name="INPUT1" id="input" onchange="calculate();"/>
<input type="hidden" name="INPUT2" id="input" value="150" />
<input type="number" name="OUTPUT" id="output">
</form>
<script type="text/javascript">
function calculate() {
var CALC1 = document.TEST.INPUT1.value;
var CALC2 = document.TEST.INPUT2.value;
var CALC3 = CALC1 + CALC2;
document.TEST.OUTPUT.value = CALC3;
}
It is taking calc1 and calc2 as a string
try using a parseInt(Calc1)+parseInt(Calc2)
This has nothing to do with java but anyway. Values are string, so you can't calculate them like that. You need to parse values to integer and then calculate.
var CALC1 = parseInt(document.TEST.INPUT1.value);

Categories