1

When the checkbox is set to false by the javascript function, the next function doesn't seem to know it has been set to false, it just ignores it.

HTML

<input type="checkbox" name="accs" id="50" class="arms"/>Arm 1: $50<br/>
<input type="checkbox" name="accs" id="60" class="arms"/>Arm 2: $60<br/>
<input type="checkbox" name="accs" id="70" class="neck"/>Neck 1: $70<br/>
<input type="checkbox" name="accs" id="80" class="neck"/>Neck 2: $80<br/>
<div id="total">$500</div>

Javascript: This function disables the checkbox according to the input class

$('.arms, .neck').change(function(){
var myClass = $(this).attr('class'); 
$('.'+myClass).not(this).prop('checked', false); 
});

Javascript next function: The block of code that processes the action if the checkbox is either enabled or disabled

var input = document.getElementsByTagName("input");
var total = document.getElementById("total");
var prev;

for(i=0;i<input.length;i++){


input[i].onchange = function(){
if (this.type != 'text'){
  if(this.checked){

$("#total").fadeOut(300);
$("#total").fadeIn(300);

prev=parseFloat(total.innerHTML.replace(/[^0-9\,]+/g,"")) + parseFloat(this.id);
         total.innerHTML = "$" + prev.formatMoney(0, ',', '.');
    }else{
$("#total").fadeOut(300);
$("#total").fadeIn(300);

prev=parseFloat(total.innerHTML.replace(/[^0-9\,]+/g,"")) - parseFloat(this.id);
         total.innerHTML = "$" + prev.formatMoney(0, ',', '.');
     }
   }
  }
}

After clicking all the checkbox you realize that it won't go back to the original price (500) but keeps on adding up.

Fiddle

The reason I haven't used radio is because the form must send the same 'input name' for all the options

Herland Cid
  • 574
  • 1
  • 6
  • 23
  • 1
    If you only want to select one arm and one neck, why aren't you using two radio groups instead of four checkboxes? – j08691 Mar 22 '13 at 20:46
  • @j08691 a radio would be the best option, but it's hard to change the format to radio for our project. It would work if every radio had the same input attribute in html: name="accesory" and that would also be compatible with first function – Herland Cid Mar 22 '13 at 20:53
  • 1
    Shame, you could reduce it (and fix the problem) with this: http://jsfiddle.net/j08691/kU6ac/2/. Plus you should really set the value in the value property, not the ID. – j08691 Mar 22 '13 at 20:55
  • 1
    id cannot start with a number (or just be a number in html 5) http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html – Mark Schultheiss Mar 22 '13 at 21:22
  • Thanks @MarkSchultheiss the other thing is that I needed to put it in ID and not value because "value" is already used, it contains the accesory "id", it's a paradox of Prestashop theme, but it works fine. – Herland Cid Mar 22 '13 at 21:26
  • Try to use some other attribute than the id property or use a data element property. - in addition using the "enabled/disabled" terms for checked/unchecked is not valid as there exits a disabled property (true/false) that actually disables the element (cannot check/uncheck and it gets a "disabled' visual effect. – Mark Schultheiss Mar 22 '13 at 22:09
  • Thanks @MarkSchultheiss do you think there's a way to mix both, the checked/unchecked and the (true and false)? I was thinking of a condition for true and false instead of checked or unchecked, but I dont know how to do it – Herland Cid Mar 22 '13 at 22:30
  • 1
    I put up one possible solution - quite different from yours, but use it for ideas if you can. – Mark Schultheiss Mar 22 '13 at 22:45

1 Answers1

1

I set up a fiddle here: http://jsfiddle.net/MarkSchultheiss/r6MXA/2/ Using this markup:

<input type="checkbox" name="accs" icost="50" class="arms" />Arm 1: $50
<br/>
<input type="checkbox" name="accs" icost="60" class="arms" />Arm 2: $60
<br/>
<input type="checkbox" name="accs" icost="70" class="neck" />Neck 1: $70
<br/>
<input type="checkbox" name="accs" icost="80" class="neck" />Neck 2: $80
<br/>
<div id="total">$500</div>

and this code:

var items = $('input.arms[type="checkbox"], input.neck[type="checkbox"]');
items.change(function () {
    var myClass = $(this).attr('class');
    $('.' + myClass).not(this).prop('checked', false);
    var total = $('#total');
    var totalcost = 0;
    total.fadeOut(300);
    items.filter(':checked').each(function (i) {
        var cost = $(this).attr('icost');
        totalcost = totalcost + parseFloat(cost);
    });
    total.text("$" + totalcost + ".00"); // fix your format here as needed
    total.fadeIn(300);
});

EDIT: manage the whole check/uncheck cycle thing with a base on the current value in the text total area.

var items = $('input.arms[type="checkbox"], input.neck[type="checkbox"]');
// get the initial total and store in a data for use later (resets as needed)
var total = $('#total');
total.data("currentvalue", parseFloat(total.text().replace(/[^0-9\,]+/g, "")));
items.change(function () {
    var currenttotal = total.data("currentvalue");
    var myClass = $(this).attr('class');
    var thisGroup = $('.' + myClass);
    var notme = $('.' + myClass + ':checked').not(this);
    var notmeCost = (notme.length ? notme.attr('icost') : ($(this).is(':checked') ? 0 : $(this).attr('icost')));

    notme.prop('checked', false);
    currenttotal = currenttotal - notmeCost;
    total.fadeOut(300);
    thisGroup.filter(':checked').each(function (i) {
        var cost = $(this).attr('icost');
        currenttotal = currenttotal + parseFloat(cost);
    });
    total.data("currentvalue", currenttotal);
    total.text("$" + currenttotal + ".00"); // fix your format here as needed
    total.fadeIn(300);
});
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
  • Thank you @MarkSchultheiss this is very useful! the only problem is that it doesn't take the starting value (500) It just adds up the accesories but exclude the whole $500 – Herland Cid Mar 22 '13 at 22:51
  • I wondered about that and had a question - can other events alter that value AFTER the checkbox are active? IF that is the case, you will need to 1. save the value (you know how), subtract the currently checked values in the group checked (see that very first part myClass thing), - I can update to do that.. – Mark Schultheiss Mar 22 '13 at 22:55
  • Done @MarkSchultheiss http://jsfiddle.net/r6MXA/10 I rescued the value, works at first round, but something weird happens after... – Herland Cid Mar 22 '13 at 23:17
  • I'm currently facing something I didn't expect but isn't so tough. There are items that can do multiple select. for example, what if i wanted to do multiple select for arms? the condition should be on this line: `if(input!=arms){notme.prop('checked', false);}` – Herland Cid Mar 24 '13 at 23:15