samedi 24 janvier 2015

Adding to variable value on condition in Javascript

I have a variable called "total" that I would like to assign a number (or price) to and then add to that number when certain options are selected or checked. Here is the code I have so far.


Here is the code that sets the initial value (base price) of the variable that will be added to. THIS CODE CURRENTLY WORKS.



$('input[name=trimlevel]').change(function(){

var total = 0;

if(document.getElementById('basetrim').checked==true) {

var total = 0;
var basetrim = 3550.00;
total = total + basetrim;
$('#myTotal').html('$' + total);

}else if(document.getElementById('upgrade').checked==true) {
var total = 0;
var upgrade = 2400.00;
total = total + upgrade;
$('#myTotal').html('$' + total);


}else {
$('#myTotal').html('$' + total);
}


});


Here is an example of the code used to "add to" the value of the variable. THIS IS NOT WORKING.



$('input[name=leather]').change(function(){

if(document.getElementById('leather).checked == true) {
var leather = 180.00;
total = total + leather;
$('#myTotal').html('$' + total);
}
});


Here is the HTML for reference



<div class="modelsel">
<h2>1. SELECT MODEL</h2> <br>
<label for="basetrim">BASE MODEL</label>
<input id="basetrim" type="radio" name="trimlevel" value="2400.00">
<label for="upgrade">UPGRADED MODEL</label>
<input id="upgrade" type="radio" name="trimlevel" value="3550.00">

</div>

<div class="inside">
<h2>3. BASE MODEL ADD-ONS</h2><br>
<input type="checkbox" value="180.00" id="leather" name="leather" />
<label for="leather">Leather Seat (+ $180.00)</label><br>
</div>

<div id="myTotal"></div>


I am relatively new to Javascript and have been struggling with this for hours. Any help is appreciated. Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire