How to divide sum by 2 if checkbox checked? javascript/html - javascript

Like written in the Title. I am trying to get the sum from some fields - this is working fine already. But I want to add a function that if the check box is checked before and I press the "Calculate" Button the sum should be divided / 2 automatically. I don't have the code I already have in the moment but if its needed I will upload later. But for now maybe its some easy thing and very general. Maybe someone can give some example already. kind regards

Since you did not provide any markup, you should do something like this, but adapted to your code:
var sum = calculateSum();
if ($("#checkbox").is(':checked')) sum = sum/2;

$('#calculateButton').on('click', function(){
// do summation and assign it to sum variable
var sum = sumCalculation();
if ($('#checkbox').is(':checked'))
{
sum = sum/2;
}
});

You may try:
$("#checkbox").change(function(){
if($(this).is(':checked')){
// do the division
}
});
Assuming your checkbox would have id="checkbox"

var chksum = document.getElementById('u r checkbox ID').checked;
if(chksum=="true")
{
sum=sum/2;
}

Try this. Note let the id of checkbox is #checkbox
var sum = 10;
$("#checkbox").change(function(){
if($(this).is(':checked')){
sum /=2;
}
alert(sum);
});

Related

Data-price error with trailing zero's

I have a simple jquery script that allows users to see total prices for the services they select on a form via checkboxes.
The price calculator is simple and works alright. However, whenever there is a trailing zero on the end of a price (ex. 14.90) it does not calculate and instead concats to the end of the price (rather it's default or other prices are selected). Is there another method to use in this situation?
Here is the issue:
https://jsfiddle.net/tn5xtfss/
var base_price = 0;
function CalculatePrice() {
var base_cost = base_price;
$(".quote--price:checked").each(function() {
base_cost += $(this).data("price");
});
$("#final_price").text(base_cost);
}
CalculatePrice();
$(".quote--price").click(function() {
CalculatePrice();
});
Parse your number
base_cost += parseFloat($(this).data("price"));
demo

Checkbox checked add to cart

So I'm having some issues with my cart. Some jQuery issues and I was wondering if someone could point me in the right direction.
Thing is that I'm trying to make the cart more dynamic, if you check in a checkbox the item gets added to the cart and the total price and discount for that item is shown too.
Just having trouble with the checkbox part.
$(document).ready(function($){
$('#cart_listing .quantity').change(function (event) {
$quan = $(this);
console.log($quan.parent().next()[0]);
$quan.parent().next().find('.price').text(function () {
return $quan.val() * parseInt($(this).attr('data-val'), 10) + ' €';
});
var total = 0;
$('#cart_listing .price').each(function(k, v){
total += parseFloat($(v).text(), 10);
});
$('#cart_listing .faster').text(function () {
faster = parseInt($(this).attr('data-val'), 10);
return $(this).attr('data-val' + ' €');
});
var discount_pct = parseInt($("#cart_listing .discount").data("val"), 10);
var discount = -(total * discount_pct/100);
$('#cart_listing .discounted').text('-' + -discount + ' €');
$('#cart_listing #total').text(total + discount + faster + ' €')
});
});
jsFiddle: http://jsfiddle.net/ooh43u6t/
I don't understand your question correctly, but I think you want to add the value for the checkbox when checked using jQuery.
$('input[type=checkbox]').click(function(){
if($(this).is(':checked')){ //use this to see if the checkbox is checked or not
//do something...
}
});
I don't know if you wanted something like this. If not, can you clarify more? Thanks!
Edit:
So, you basically use the same thing, something like this perhaps,
if($(this).is(':checked')){
console.log($(this).parent('td').siblings('td').html()); // check this, you'll have the item name. (the first td)
console.log($(this).attr('data-val')); // this will have the value on the "data-val" attribute on your checkbox
}
So, now you have the name of the product (1st log) and the price (2nd log).
Now keep in mind that this would work only if the format is going to be consistent, and also instead of doing a generic input[type=checkbox], you might want to give all the product checkboxes a unique class, so it doesn't interfere with other checkboxes you might have. Hope this helps. If not, I'd be happy to answer some more. Thanks!

Each input values into each <span> or <div>

I am trying to get the value of each input and put it in the span tag below without using ID's or classes. It's easy if I use classes or Id's. But I need to be able to use it without those. I need each input values put into each span tag. I have two but it will be more in actuality.
Thanks in advance.
any help is appreciated
below is my fiddle
var input = $('input:text');
$(input).each(function(){
values = $(this).val();
});
$('b').each(function(){
$(this).html(values);
console.log(values);
});
http://jsfiddle.net/6LB76/3/
Simple enough, just juggle the index.
jQuery
$('input:text').each(function(i){//get index for each input
var tx = $(this).val();//get value for this input
$('b').eq(i).html(tx);//print value on output with same index
});
Which can easily be expanded to update irt, as you can see in this fiddle...
You need to loop through ieach input and then each output. This is assuming that you have exactly the same number of input fields and spans to show the values.
Here is the code:
var input = $('input:text');
var values = new Array();
$(input).each(function (i) {
values[i] = $(this).val();
});
$('b').each(function (i) {
$(this).html(values[i]);
console.log(values[i]);
});
Here is the Fiddle:
First, set a counter variable i to 0. The loop through each <b> element and increment i as we go through each. Populate the <b> element with the value of the input at index i.
function run() {
var inputs = $('input:text');
var i = 0;
$('b').each(function(){
$(this).html(inputs.eq(i++).val());
});
}
Here is the Fiddle
You're being somewhat vague in your question, so please clarify if this is not what you want.

Getting NaN during calculation

I am trying to calculate the total of a selection of CDs in addition of P&P. The code I'm using is coming to a total of NaN?
Very confused here. what am I doing wrong?
function calculateTotal() {
var Collection method = document.getElementById('collection').value +
var select CDs = document.getElementById('selectCD').value;
var total = document.getElementById('total');
total.value = 'collection'(total) + 'selectCD'(total);
}
Here is a JSFiddle with the full code.
In your fiddle collection and selectCD are divs (not inputfields) containing inputfields. You can't do divElm.value.
Then the php-code in your fiddle would normally be able to output more then one cd, so you'd need to add the totals of the selected cd's to.
The minimum changes needed to get your code working are:
function calculateTotal(){
var coll = document.getElementsByName('deliveryType'),
cds = document.getElementsByName('cd[]'),
cdTot = 0,
L = coll.length;
while(L--){if(coll[L].checked){ //get shipping costs
coll=Number(coll[L].getAttribute('title')); break;
} }
L=cds.length;
while(L--){if(cds[L].checked){ //add total prices
cdTot += Number(cds[L].getAttribute('title'));
} }
// output total
document.getElementById('total').value = coll + cdTot;
}
Also you'd want to set some more triggers to function calculateTotal (from the shipping costs and selected cd's; this way, if they change, the total-field will update to).
See this working fiddle with these changes (and some other fixes) based on your fiddle so you can get motivated seeing it (calculation) in action.
However I do hope this is for a school-question and not for a live webshop. I would re-think my strategy as I think you are currently working your way to a big security hole.
Good luck!

How to add up values of checkboxes in real time using JQuery?

Should be easy, but this code isn't working for me:
$("#checkboxdiv input:checkbox").change(function() {
var total = 0;
$('#checkboxdiv input:checkbox:checked').each(function() {
total += parseInt(this.value, 10);
});
$('#totalsdiv').text(total);
});
The checkboxes to consider are all in the #checkbox div, and I'm trying to fill in the div with the total amount.
Thanks.
As the code is fine and works, it's probable there is an unrelated error elsewhere.
You should use the developer tools to debug and hunt it.
You could try to listen for the click event on the CheckBox, instead of the change event.
Without knowing your problem I'll guess at this
$("#checkboxdiv input:checkbox").change(function() {
var total = 0;
$('#checkboxdiv input:checkbox:checked').each(function() {
total += parseInt($(this).val(), 10);
});
$('#totalsdiv').text(total);
});

Categories