-4

I have a simple form with a few checkbox options and a submit button. What I would like to accomplish is that when a visitor selects one or more options, the total price (sum) will be shown after clicking the button.

The total (sum) must be placed in the last div with id 'uk-total-price'. The basic value is the price without extra options. The basic value, in this case 19.500, is visible.

Is it possible with javascript or jquery to make this work?

<form>

<div id="uk-form-checkbox">
<input id="option 1" class="uk-checkbox-box" value="100" name="option 1" type="checkbox">
<label for="option 1" >option 1</label>
<input id="option 2" class="uk-checkbox-box" value="250" name="option 2" type="checkbox">
<label for="option 2" >option 2</label>
<input id="option 3" class="uk-checkbox-box" value="500" name="option 3" type="checkbox">
<label for="option 3" >option 3</label>
</div>

<div id="uk-form-submit">
<button id="submit" class="uk-submit-button" type="submit">Calculate Total</button>
</div>

<div id="uk-total-price">19.500</div>

</form>
Viktor Horst
  • 3
  • 1
  • 3

1 Answers1

1

I think that if you just want to print the result you can use simple javascript code.The submit button will refresh your page.I changed a little bit your HTML code and I wrote the js code.Just copy my answer and paste it in your HTML file.Sorry for any English mistakes...Hope this helps! :)

<form>
<div id="uk-form-checkbox">
<input id="option-1" class="uk-checkbox-box" value="100" name="option 1" 
type="checkbox">
<label for="option-1" >option 1</label>
<input id="option-2" class="uk-checkbox-box" value="250" name="option 2" 
type="checkbox">
<label for="option-2" >option 2</label>
<input id="option-3" class="uk-checkbox-box" value="500" name="option 3" 
type="checkbox">
<label for="option-3" >option 3</label>
</div>
</form>

<div id="uk-form-submit" style="cursor:pointer;">
<div id="submit" class="uk-submit-button">Calculate Total</div>
</div>
<div id="uk-total-price"></div>

<script>
var button = document.getElementById("submit");
var result = document.getElementById("uk-total-price");

button.addEventListener( "click" , function(){

var options = document.getElementsByClassName('uk-checkbox-box');
var calc = 0;
var totalPrice = 19.5;    

 for (i = 0; i < options.length; i++) {
  if (options[i].checked) {
    calc = calc + parseInt( options[i].value );
  }
}
result.innerHTML = calc + totalPrice;

});
</script>