jeudi 2 mai 2019

start a function if another function is active and activate some checkboxes only - JQuery

i Have a function that I want to start only if another function is previously activated.

I have some checkboxes and I need to sum its values to get the total.

Only When a user has selected some of the checkboxes it must activate another checkbox with a discount. I want that the discount checkbox get activated after the first selection because, if I don't do so, I could have a negative price.

Then (if it's possible) I want that the discount checkbox get deactivated is a user deselect all the previous checkboxes.

Is this possible?

Here's my script. I'm super new in Javascript/jQuery so this might be a stupid question.

Thank you

$(document).on('change', getCheck);
  
  function getCheck() {
    var total= 0;
    $('[type="checkbox"]:checked').not("#discount").each(function(i, el) {
    //console.log($(this).not("#off").val());
    var SumVehicle = parseFloat($(el).val());
    total += SumVehicle;
    console.log(total);
    //console.log(price_tot);
    $('#rata').text(total  +" €");

     var finalprice = total;
     var Check = function getCheck(); 
         
        if(typeof(Check) != "undefined" && Check !== null) {
          $("#discount").toggle() {
          var Discount = parseFloat($(this).val());
          finalprice -= Discount;
          console.log(finalprice);
          $('#rata').text(finalprice  +" €");
        });
      });
    };
  };
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" name="vehicle1" value="1000"> I have a bike<br>
<input type="checkbox" name="vehicle2" value="2000"> I have a car<br>
<br><br><br>
<input id="discount" type="checkbox" name="discount" value="-200"> Discount<br>

<div id="rata">rata</div>



Aucun commentaire:

Enregistrer un commentaire