javascript - if part not executed -


$(document).ready(function () {     var t=true;     var f=false;     var cheap;     $('.day1').on('change', function (e) {         if($(this).val() == "saturday"){             cheap = true;         }         else{             cheap=false;         }     });     if(cheap==true){         $('.pricing1').change(function () {         var price = parsefloat($('.total').data('base-price')) || 0;         $('.pricing1').each(function (i, el) {             price += parsefloat($('option:selected', el).data('cheap'));             $('.total').val('$' + price.tofixed(2));         });         //console.log('cheap',cheap)         });     }     else{         $('.pricing').change(function () {         var price = parsefloat($('.total').data('base-price')) || 0;         $('.pricing').each(function (i, el) {             price += parsefloat($('option:selected', el).data('price'));             $('.total').val('$' + price.tofixed(2));         });         console.log('cheap',cheap)         });     }  }); 

the console reading returns true cheap when saturday selected. if part not executed. every time else part executed. logically should execute if part if cheap true. , console displays cheap value true value of cheap true. weird!

you registering event handlers @ dom ready, @ point of time cheap has value false if condition not satisfied change handler in else part registered.

$(document).ready(function () {     var t = true;     var f = false;     var cheap;     $('.day1').on('change', function (e) {         if ($(this).val() == "saturday") {             cheap = true;         } else {             cheap = false;         }     });     $('.pricing1').change(function () {         if (cheap == true) {             var price = parsefloat($('.total').data('base-price')) || 0;             $('.pricing1').each(function (i, el) {                 price += parsefloat($('option:selected', el).data('cheap'));                 $('.total').val('$' + price.tofixed(2));             });             //console.log('cheap',cheap)         } else {             var price = parsefloat($('.total').data('base-price')) || 0;             $('.pricing').each(function (i, el) {                 price += parsefloat($('option:selected', el).data('price'));                 $('.total').val('$' + price.tofixed(2));             });             console.log('cheap', cheap)         }     });  }); 

you can simplify code like

$(document).ready(function () {     var t = true;     var f = false;     var cheap;     $('.day1').on('change', function (e) {         if ($(this).val() == "saturday") {             cheap = true;         } else {             cheap = false;         }     });     $('.pricing1').change(function () {         var data = cheap ? 'cheap' : 'price';         var price = parsefloat($('.total').data('base-price')) || 0;         $('.pricing1').each(function (i, el) {             price += parsefloat($('option:selected', el).data(data)) || 0;         });         $('.total').val('$' + price.tofixed(2));     });  }); 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -