Skip to main content
Legend
March 23, 2017
Answered

jQuery problem - simple checkbox calculation

  • March 23, 2017
  • 2 replies
  • 626 views

Ok Guys and Girls I need your help. Should be simple but I am not seeing clearly at the moment.

What I want to do is when a checkbox is clicked the discount price gets removed from the current total. If I have rounded up figures like below 35.00 and the discount price 15.00 it works ok. Clicking and unclicking the checbox functions. As soon as I use something like 35.50 and 15.50 it goes haywire. I know its something to to with parseInt( ) rounding down the numbers BUT if I remove that then not even the rounded up figures work.

Full test code below. There's probably a better way to do this.

<!DOCTYPE html >

<html>

<head>

<meta charset="UTF-8" />

<title>Price Discount</title>

<script  src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script>

// CHANGE PRICING FOR CHILD PRICE

$(document).ready(function() {

$('.select_child').click(function() {

if($(this).is(':checked')) {

var child_discount = $('.child_discount').val();

var current_total = $('.total span').text();

var new_total = current_total - child_discount;

$('.total span').text(new_total.toFixed(2));

}

else {

var child_discount = parseInt($('.child_discount').val());

var current_total = parseInt($('.total span').text());

new_total = current_total + child_discount;

$('.total span').text(new_total.toFixed(2));

}

});

});

</script>

</head>

<body>

<div class="child_price" style="padding: 15px 25px;">

<input type="checkbox" title="select_child" class="select_child" value="child">Select if Child (under 16) - Concession Price &pound;20.00

<input type="text" title="child_discount" name="child_discount" class="child_discount" value="15.00">

</div>

<!-- end child_price -->

<p class="total">TOTAL &pound;<span>35.00</span></p>

<!-- end total -->

</body>

</html>

    This topic has been closed for replies.
    Correct answer osgood_

    I thnk the answer may be use parseFloat instead of parseInt - I haven't thoroughly tested it yet so if anyone has any better solution let me know.

    Os

    2 replies

    Rob Hecker2
    Legend
    March 23, 2017

    Integers are whole numbers. I don't handle monetary decimals  with javascript/jquery (not that there is any reason not to).

    I use a HTML pattern like so:

    <input name='amount' type='number' pattern='[0-9]+([\.|,][0-9]+)?' step='0.01' style='width:90%' />

    And I store the numbers in the database as decimal(8,2).

    osgood_Author
    Legend
    March 23, 2017

    https://forums.adobe.com/people/Rob+Hecker2  wrote

    Integers are whole numbers. I don't handle monetary decimals  with javascript/jquery (not that there is any reason not to).

    I use a HTML pattern like so:

    <input name='amount' type='number' pattern='[0-9]+([\.|,][0-9]+)?' step='0.01' style='width:90%' />

    And I store the numbers in the database as decimal(8,2).

    Right, anyway I think the minor issue is solved now after some trial and error. I'm just a bit confused as to why some values are retrieved correclty without parseInt and pasreFloat and others arent. I'm not using it in the  first insatnce of getting the information but it doesnt work without it being present in the second instance.

    Oh well it works........so that's another issue ironed out. Just been thrown a last minute price option to add into the equation, which means reconfiguring a lot of the code, to take it into account. Its a lot of work and more problems to accomodate the only 2 departures which feature a childs price!!! If I'd known at the outset I'd probably would have done it a different way, instead of hacking it.

    osgood_AuthorCorrect answer
    Legend
    March 23, 2017

    I thnk the answer may be use parseFloat instead of parseInt - I haven't thoroughly tested it yet so if anyone has any better solution let me know.

    Os