I am trying to calculate all fields in the form which have the same class (shown below in the code), I have managed to get this to work, I think I may be setting the wrong value types in javascript as its not adding them together correctly. Its adding them on top of each other and not to each other. so instead of 4.00 + 5.50 equalling to 9.50 it would read 04.0005.50.
function calculate() {
var total = 0;
$('input.calc').each(function() {
var num = this.value;
//Number(this.value, 10);
if (!isNaN(num)) {
total += num.toFixed(2);
}
});
$("#total").val(total);
}
Any help its appreciated, Thanks
<script language="javascript">
function calculate() {
var total = 0;
$('input.calc').each(function() {
var num = this.value;
//Number(this.value, 10);
if (!isNaN(num)) {
total += num.parseFloat(num);
}
});
$("#total").val(total);
}
</script>
function calculate() {
var total = 0;
$('input.calc').each(function() {
var num = parseFloat(this.value);
//Number(this.value, 10);
if (!isNaN(num)) {
total += num;
}
});
$("#total").val(total);
}
Related
I'm trying to make a form that contains several different products with different options to calculate and I need to get the total price.
Scenario:
(a+b) * c = price
(a+b) * c = price
(a+b) * c = price
price + price + price = toltal
This is my script, but it doesn't work
$(document).ready(function () {
$("#form").on("keyup", ".form-calc", function () {
var parent = $(this).closest(".items");
var sum = parent.find(".form-line").val((parent.find(".form-cost").val() + parent.find(".form-cost-skift").val()).toFixed(2));
parent.find(".form-line").val((parent.find(".form-qty").val() * sum).toFixed(2));
var total = 0;
$(".form-line").each(function () {
total += parseFloat($(this).val() || 0);
});
$("#total").val(total.toFixed(2));
});
});
JSFiddle
What I'm missing?
Maybe something like this? https://jsfiddle.net/tnzcz8a3/3/
$(document).ready(function () {
$("#form").on("keyup", ".form-calc", function () {
var parent = $(this).closest(".items");
var sum = 0;
var total = 0;
if (!isNaN(parent.find(".form-cost").val())) sum += +parent.find(".form-cost").val();
if (!isNaN(parent.find(".form-cost-skift").val())) sum += +parent.find(".form-cost-skift").val();
if (!isNaN(parent.find(".form-qty").val())) sum *= +parent.find(".form-qty").val();
parent.find(".form-line").val(sum.toFixed(2));
$(".form-line").each(function () {
total += parseFloat($(this).val() || 0);
});
$("#total").val(total.toFixed(2));
});
});
I want to get the average by input radio button value.
I have a form with 3x 10 radio fields counting from 1 to 10.
Now I want to calculate the average, so you get a final score.
I get it working with a text field, but I don't know how to do it with radio buttons checked.
This is my javascript:
$('.beoordeling').keyup(function () {
var total = 0,
valid_labels = 0,
average;
$('.beoordeling').each(function () {
var val = parseInt($(this).val(), 10);
if (!isNaN(val)) {
valid_labels += 1;
total += val;
}
});
console.log(total)
average = total / valid_labels;
$('.eindstand').val(average);
});
I have created a jsfiddle with how I have it now.
http://jsfiddle.net/Q2MEq/
Demo Fiddle
Check this code.
$('.beoordeling').change(function () {
var total = 0,
valid_labels = 0,
average;
$('.beoordeling:checked').each(function () {
var val = parseInt($(this).val(), 10);
if (!isNaN(val)) {
valid_labels += 1;
total += val;
}
});
console.log(total)
average = total / valid_labels;
$('.eindstand').val(average);
});
Use change events in case of inputs like radio and not keyup.
use :checked selector to get selected radio buttons only.
See for this you need to have radio buttons with different name attributes or without it, and then you can change to this:
$(':radio').change(function () {
var total = 0,
valid_labels = 0,
average;
$(':checked').each(function () {
var val = parseInt($(this).val(), 10);
if (!isNaN(val)) {
valid_labels += 1;
total += val;
}
});
console.log(total)
average = total / valid_labels;
$('.eindstand').val(average);
});
Demo
Please check this
$('.beoordeling').keyup(function () {
var total = 0,
valid_labels = 0,
average;
$('.beoordeling').each(function () {
var val = parseInt($(this).val(), 10);
if (!isNaN(val)) {
valid_labels += 1;
total += val;
}
});
console.log(total)
average = total / valid_labels;
$('.eindstand').val(average);
});
Demo: JSFiddle
You can determine whether or not a radio button is checked with ".checked"
document.getElementById("radio_1").checked
... will return true if the button is checked, false otherwise.
I have used a jsFiddle that I found on another question on here that will work perfectly if I can get the subtotal to multiply instead of addition.
I am sure this is a really easy fix, but I'm struggling to make the change.
Can someone help me please?
Thank you in advance
http://jsfiddle.net/77uxK/48/
$(document).ready(function(){
var sum = 0;
function calcSum(prevVal){
sum = sum - prevVal + (this.value/1);
return sum;
}
var subAmt = $("#sub"), taxAmt = $("#tax"), totAmt = $("#total");
$(".val").each(function(){
var prevVal = this.value/1, self = this;
$(this).keyup(function(){
subAmt.val(calcSum.call(self, prevVal));
totAmt.val(sum + sum*parseFloat(taxAmt.val()/100));
prevVal = self.value;
});
});
});
just change the function to
function calcSum(prevVal) {
var val1 = $('#val1').val();
var val2 = $('#val2').val();
sum = parseInt(val1) * parseInt(val2); // parseFloat based on your need change it
return sum;
}
jsfiddle link
im a noob at javascript and was wondering how it is possible to display an alert message when the total value goes below 0. The page in question is here (http://cashmoneypaid.com/order-form-phones-work) and the script i have tried is below. Thanks so much for the help.
function checkTotal() {
document.listForm.total.value = '';
var sum = 20;
for (i = 0; i < document.listForm.choice.length; i++) {
if (document.listForm.choice[i].checked) {
sum = sum + parseInt(document.listForm.choice[i].value);
}
}
document.listForm.total.value = sum;
}
$("#total").ready(function() {
if (sum > 0) {
alert("We are sorry, but at this time we can not offer you any money for your item");
}
});
Since you use jQuery change it all!!
$(function(){
var sum = 20;
$('form input[type="checkbox"]').change(function(){
if (this.checked)
sum -= parseFloat(this.value);
else
sum += parseFloat(this.value);
if (sum < 0)
alert('bla bla bla');
});
});
Live DEMO
You can also use this trick:
$('form input[type="checkbox"]').change(function(){
var value = parseFloat(this.value) * (this.checked ? -1 : 1);
sum += value
...
Live DEMO
I am trying to find the sum of checkbox values (23.75 and 142.75)
Poaten
Checkbox1: 2012-01-17, Porti, 1.760, 23.75
Checkbox2: 2012-01-17, Kopien, 10.560, 142.55
Checkbox3: 2012-01-17, Honorar, 33.600, 453.60
Checkbox4: 2012-01-17, Telefon, 0.640, 8.65
The output is in "Restbetrag". I used the following function but I receive the sum of primary key's value of selected checkbox items in "Posten".
In posten I see four values for each checkbox which are separated by comma. Where should I start to have the 4th value of each clicked checkbox (23.75, 142.55...)? Could you please advice where to find a similar solutions?
Thanks
mpol
function showTotal() {
document.frechnungenadd.x_Restbetrag.value = '';
//document.write("test");
var sum = 0;
var elements = document.getElementsByName("x_Posten[]");
for (i=0;i < elements.length;i++) {
if (elements[i].checked) {
sum = sum + +elements[i].value;
}
}
document.frechnungenadd.x_Restbetrag.value = sum;
}
Not sure if this is what you are looking for...
if (elements[i].checked) {
var myarr = elements[i].value.split(",");
sum += parseFloat(myarr[3]);
}
I solved my problem. Thanks for your support. Here your the code for solution.
Regards
mpol_ch
function showTotal() {
document.frechnungenadd.x_Summe.value = '';
document.frechnungenadd.x_MwSt.value = '';
//document.write("test");
var Summe = 0;
var MwSt = 0
var splitted
var elements = document.getElementsByName("x_Posten[]");
for (i=0;i < elements.length;i++) {
if (elements[i].checked) {
splitted = elements[i].nextSibling.nodeValue.split(",");
MwSt = MwSt+ + parseFloat(splitted[2]);
Summe = Summe+ + parseFloat(splitted[3]);
}
}
document.frechnungenadd.x_MwSt.value = MwSt.toFixed(2);
document.frechnungenadd.x_Summe.value = Summe.toFixed(2);
}
setInterval('showTotal()',1000);