How to go about calculating the sum of two fields on change?
My HTML
<div class="field">
<label for="field1">F1:</label>
<input type="number" name="field1" min="0" placeholder="0" Title="Only positive numbers" required></div>
<div class="field">
<label for="field2">Field2:</label>
<input type="number" name="field2" disabled>
</div>
And i got this but unsure on how this works/how to get it to work..
<script> $(".input").on("change", function(){
var ret = Number($(".field1").val()) + Number($(".field2").val());
$("#field3").val(ret);
Is there an easier way to do this?
Your way is fine, But I prefer using on input event handler, as it's faster in updating the total number.
Also if you like to expand the sum process to handle many textbox(s) you can do the following
$(document).ready(function(){
$(".parameter").on("input",function() {
var total=0;
$(".parameter").each(function(){
if(!isNaN(parseInt($(this).val())))
{
total+=parseInt($(this).val());
}
});
$(".total").val(total);
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="field">
<label for="field1">F1:</label>
<input type="number" class="parameter" name="field1" min="0" placeholder="0" Title="Only positive numbers" required></div>
<div class="field">
<label for="field2">F2:</label>
<input type="number" class="parameter" name="field2" min="0" placeholder="0" Title="Only positive numbers" required>
</div>
<div class="field">
<label for="field2">F3:</label>
<input type="number" class="parameter" name="field3" min="0" placeholder="0" Title="Only positive numbers" required>
</div>
<div class="field">
<label for="field2">Total:</label>
<input type="number" class="total" name="total" min="0" placeholder="0" Title="Only positive numbers" required>
</div>
By using this way you can include as many as number fields in the sum, and all what you need to do is to add class="parameter" to this new number field.
You can set the same class to the two inputs and then on the class change you can get the values of those inputs by their ids (which you can also set), and simply add them.
Something like:
<input id="input1" class="myclass"/>
<input id="input2" class="myclass"/>
And then on the javascript side:
$(".myclass").on("change", function(){
var value1 = document.getElementById('input1').value;
var value2 = document.getElementById('input2').value;
var sum = parseInt(value1) + parseInt(value2);
});
Any change in input will trigger the .on()'s callback function. Inside get the value of field1and field2 and parse it into int as they will be a string. Add them both and insert into field3
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="field">
<label for="field1">F1:</label>
<input type="number" id="field1" min="0" placeholder="0" Title="Only positive numbers" required/>
</div>
<div class="field">
<label for="field2">Field2:</label>
<input type="number" id="field2"/>
</div>
<input type="number" disabled id="field3"></input>
<script>
$("input").on("change", function() {
var ret = parseInt($("#field1").val()) + parseInt($("#field2").val() || '0')
$("#field3").val(ret);
})
</script>
Qty1 : <input onblur="findTotal()" type="text" name="qty" id="qty2"/><br>
Qty2 : <input onblur="findTotal()" type="text" name="qty" id="qty2"/><br>
Qty3 : <input onblur="findTotal()" type="text" name="qty" id="qty3"/><br>
Qty4 : <input onblur="findTotal()" type="text" name="qty" id="qty4"/><br>
Qty5 : <input onblur="findTotal()" type="text" name="qty" id="qty5"/><br>
Qty6 : <input onblur="findTotal()" type="text" name="qty" id="qty6"/><br>
Qty7 : <input onblur="findTotal()" type="text" name="qty" id="qty7"/><br>
Qty8 : <input onblur="findTotal()" type="text" name="qty" id="qty8"/><br>
<br><br>
Total : <input type="text" name="total" id="total"/>
<script type="text/javascript">
function findTotal(){
var arr = document.getElementsByName('qty');
var tot=0;
for(var i=0;i<arr.length;i++){
if(parseInt(arr[i].value))
tot += parseInt(arr[i].value);
}
document.getElementById('total').value = tot;
}
</script>
link:-How get total sum from input box values using Javascript?
or
link:-http://www.javascript-coder.com/javascript-form/javascript-calculator-script.phtml
Convert String to Number using + operator so +"0" === 0 and +"1" + +"2" === 3
$(".input").on("keyup change click", function() {
var ret = +$("[name=field1]").val() + +$("[name=field2]").val();
$("[name=field3]").val(ret);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="field">
<label for="field1">Field1:</label>
<input class='input' type="number" name="field1" value='0' min="0" placeholder="0" Title="Only positive numbers" required>
</div>
<div class="field">
<label for="field2">Field2:</label>
<input class='input' type="number" name="field2" value='0' min="0" placeholder="0">
</div>
<div class="field">
<label for="field3">Result:</label>
<input type="number" name="field3" disabled>
</div>
Related
I'm using this jQuery script to focus onto next input when enter pressed:
$(function() {
$(".form >input").on('keyup', function(e) {
if (e.which === 13) {
$(this).next('input').focus();
}
});
});
It does work with this basic skinny HTML form
<div class="form">
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
</div>
But not with this one
<div class="form">
<input name="cislo1" type="text" class=cislo1 placeholder="" id="cislo1" autofocus /><br>
<input name="cislo2" type="text" class=cislo2 placeholder="" id="cislo2" /><br>
<input name="cislo3" type="text" class=cislo3 placeholder="" id="cislo3" /><br>
<input name="cislo4" type="text" class=cislo4 placeholder="" id="cislo4" /><br>
<input name="cislo5" type="text" class=cislo5 placeholder="" id="cislo5" /><br>
<input name="cislo6" type="text" class=cislo6 placeholder="" id="cislo6" /><br>
<input name="cislo7" type="text" class=cislo7 placeholder="" id="cislo7" /><br>
<input name="cislo8" type="text" class=cislo8 placeholder="" id="cislo8" /><br>
<input name="cislo9" type="text" class=cislo9 placeholder="" id="cislo9" /><br>
<input name="cislo10" type="text" class=cislo10 placeholder="" id="cislo10" /><br>
<input name="cislo11" type="text" class=cislo11 placeholder="" id="cislo11" /><br>
</div>
What seems to be the problem here? Help me please, I'm stuck
The issue here is you are using direct child selector like:
$(".form > input")
This only matches any direct input element inside form class, but in your next example input is not the direct child. So, you need to descent sector instead like:
$(".form input")
This will match any input inside the form class where it is a direct child or not.
Also, you need to replace
$(this).next('input').focus();
with this:
$(this).nextAll('input:first').focus();
This is needed because .next() will get the immediately following sibling of each element, but here input is not the immediate sibling. We also have some <br> in between which are causing the issue. This can be resolved using .nextAll('input:first') instead.
Demo:
$(function() {
$(".form input").on('keyup', function(e) {
if (e.which === 13) {
$(this).nextAll('input:first').focus();
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form">
<p class="cislo">
<input name="cislo1" type="text" class=cislo1 placeholder="" id="cislo1" autofocus onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" oninput="cislo1.value=cislo1.value.slice(0,2)" /><br>
<input name="cislo2" type="text" class=cislo2 placeholder="" id="cislo2" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" oninput="cislo2.value=cislo2.value.slice(0,2)" /><br>
<input name="cislo3" type="text" class=cislo3 placeholder="" id="cislo3" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" oninput="cislo3.value=cislo3.value.slice(0,2)" /><br>
<input name="cislo4" type="text" class=cislo4 placeholder="" id="cislo4" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" oninput="cislo4.value=cislo4.value.slice(0,2)" /><br>
</p>
</div>
I am working on a an angular2 project and i setted the value of html element (input) value to 0 as follows
<div class="form-group">
<label for="balance">Balance</label>
<input ngModel type="number" class="form-control" id="balance" placeholder="Balance" name="balance" value="0" required>
</div>
but when i run the project i get empty input.
any help?
<div class="form-group">
<label for="balance">Balance</label>
<input type="number" class="form-control" id="balance" placeholder="Balance" name="balance" [(ngModel)]="balance" required>
</div>
In your component constructor
balance:number;
constructor(){
this.balance = 0;
}
I am using Formspree (which is awesome) except: I have set all my fields to required, when I try manually I can't submit a form without filling everything properly, and however I still get a bunch of blank forms coming through. Has anybody experienced / fixed this before?
Thanks!
<form action="https://formspree.io/XXXXXXXX" method="post">
<input name="_gotcha" style="display: none" type="text">
<input class="form-control input-lg" name="firstname" placeholder="First Name" required type="text">
<input class="form-control input-lg" name="lastname" placeholder="Last name" required type="text">
<input class="form-control input-lg" name="phone" placeholder="Phone number" required type="text">
<select required name="country" class="form-control input-lg" style="border-radius:none; -webkit-appearance: none; padding-top:0px;">
<option value="" disabled selected>Please select your country </option>
<option value="Afganistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="other">Not in the List</option>
<!-- etc -->
</select>
<input type="hidden" name="referrer" id="referrer_field" />
<input type="hidden" name="url" id="url_field" />
<button id="demo" type="submit">GO</button>
And this is another Solution for your code, as this will work even if somebody put "spaces or leave it with blank" in the field of Name or Phone.
Check the Fiddle..
// Bind the event handler to the "submit" JavaScript event
$('form').submit(function () {
// Get the Login Name value and trim it
var fname = $.trim($('#fname').val());
var lname = $.trim($('#lname').val());
var phon =$.trim($('#phon').val());
// Check if empty of not
if (fname === '') {
alert('First name is empty.');
return false;
}
else if (lname === '') {
alert('Last Name is empty.');
return false;
}
else if (phon === '') {
alert('Phone is empty.');
return false;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<form action="https://formspree.io/XXXXXXXX" method="post">
<input name="_gotcha" style="display: none" type="text">
<input class="form-control input-lg" name="firstname" placeholder="First Name" id="fname" required type="text">
<input class="form-control input-lg" name="lastname" placeholder="Last name" id="lname" required type="text">
<input class="form-control input-lg" name="phone" placeholder="Phone number" id="phon" required type="text">
<select name="country" class="form-control input-lg" style="border-radius:none; -webkit-appearance: none; padding-top:0px;">
<option value="" disabled selected>Please select your country </option>
<option value="Afganistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="other">Not in the List</option>
<!-- etc -->
</select>
<input type="hidden" name="referrer" id="referrer_field" />
<input type="hidden" name="url" id="url_field" />
<button id="demo" type="submit">GO</button>
If somebody type space to the input box then this Jquery code will work and ask him to fill proper text in fields.
Try W3Schools example of
http://www.w3schools.com/tags/att_input_required.asp
<input class="form-control input-lg" name="firstname" placeholder="First Name" required >
with no equals or anything
I thought the required tag was
required="True/false"
A simple solution would be something like this.
You can do this by using JQuery
// Bind the event handler to the "submit" JavaScript event
$('form').submit(function () {
// Get the Login Name value and trim it
var name = $.trim($('#log').val());
// Check if empty of not
if (name === '') {
alert('Text-field is empty. You cannot leave is blank!');
return false;
}
});
.text-label {
color: #cdcdcd;
font-weight: bold;
}
#pwd{
margin:10px 18px;
}
#logBtn{
margin:10px 90px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<form action="login.php" method="post">
<label>Login Name:</label>
<input type="text" name="email" id="log" />
<br/>
<label>Password:</label>
<input type="password" name="password" id="pwd" />
<br/>
<input type="submit" id="logBtn" name="submit" value="Login" />
</form>
I have a problem with a html5 output. Using autosum, when I remove a number, the output shows not a number in the textbox NaN.
How do I remove it?
<form method="POST" oninput="result.value=parseInt(value1.value)+parseInt(value2.value)">
<input type="number" id="value1" value="0"> +
<input type="number" id="value2" value="0"> =
<output name="result" for="value1 value2"></output>
</form>
I've had a play on JSfiddle, is this what you're after? If the value for parseInt is not an int then it uses 0.
<form method="POST" oninput="result.value=(parseInt(value1.value) || 0)+(parseInt(value2.value) || 0)">
<input type="number" id="value1" value="0"> +
<input type="number" id="value2" value="0"> =
<output name="result" for="value1 value2"></output>
</form>
https://jsfiddle.net/5acLpxqs/
Use this it fixes the issue
<form method="POST" oninput="result.value=parseInt(value1.value?value1.value:0)+parseInt(value2.value?value2.value:0)">
<input type="number" id="value1" value="0"> +
<input type="number" id="value2" value="0"> =
<output name="result" for="value1 value2"></output>
</form>
Hi I have below form field to enter Tax Id. I was wondering how can i split it so that first box takes only 2 digits an than second 7 with "-" i.e 44-2234233
<div class="form">
<label class="form1" for="o_taxId">Tax Id</label>
<input type="number" class="medium" id="o_taxId" name="taxId" />
</div>
Please help. Thanks
With JQuery you can do this:
<div class="form">
<label class="form1" for="o_taxId">Tax Id</label>
<input type="number" class="medium" id="o_taxId" name="taxId" size="2" maxlength="2" />
<input type="number" class="medium" id="o_taxId2" name="taxId2" size="7" maxlength="7" />
</div>
<script>
$('#o_taxId').bind('input', function() {
var length1 = $(this).val().length;
if (length1 === 2) {
$('#o_taxId2').focus();
}
});
</script>
this swaps focus autocratically to the second field when you type in the first, but if you don't need that you can remove the script.