javascript - How to calculate totals based on current state of divs -
what have house plan had javascript overlaid divs.
i want create function calculates figures based on whether these div ids displayed or not , return answer.
here jsfiddle https://jsfiddle.net/offtkdqk/
function calculate_total(ppa, cpa, cpo, gar) { var tot_price = 279500; var = document.getelementbyid(ppa); if (a.style.display == 'block') var ppa_tot = 1000; var b = document.getelementbyid(cpa); if (b.style.display == 'block') var cpa_tot = 5000; var c = document.getelementbyid(cpo); if (c.style.display == 'block') var cpo_tot = 6000; var d = document.getelementbyid(gar); if (d.style.display == 'block') var gar_tot = 9000; var estimate = tot_price + ppa_tot + cpa_tot + cpo_tot + gar_tot; return estimate(); }
and html:
<div id="main-bg" class="main-img"> <img src="images/house.jpg" width="100%"> </div> <div id="main-ppa" class="main-img"> <img src="images/house-paved-patio.png" width="100%"> </div> <div id="main-gar" class="main-img"> <img src="images/house-garage.png" width="100%"> </div> <div id="main-cpo" class="main-img"> <img src="images/house-covered-porch.png" width="100%"> </div> <div id="main-cpa" class="main-img"> <img src="images/house-covered-patio.png" width="100%"> </div> <div id="estimate"></div> <script> document.getelementbyid("estimate").innerhtml = "your estimated total $" + calculate_total('main-ppa', 'main-cpa', 'main-cpo', 'main-gar'); </script>
with css like:
#main-ppa {display:none;} #main-gar {display:block;} #main-cpo {display:block;} #main-cpa {display:none;}
any appreciated.
check fiddle https://jsfiddle.net/c89rwy8y/1/
i changed @brain_bacon answer little bit computed style
function calculate_total(ppa, cpa, cpo, gar) { var estimate = 0; if (getdisplay(document.getelementbyid(ppa)) == 'block') { estimate += 1000; } if (getdisplay(document.getelementbyid(cpa)) == 'block') { estimate += 5000; } if (getdisplay(document.getelementbyid(cpo)) == 'block') { estimate += 6000; } if (getdisplay(document.getelementbyid(gar)) == 'block') { estimate += 9000; } return estimate; } function getdisplay(element) { return element.currentstyle ? element.currentstyle.display : getcomputedstyle(element, null).display; } document.getelementbyid("estimate").innerhtml = "your estimated total $" + calculate_total('main-ppa', 'main-cpa', 'main-cpo', 'main-gar');
you can check limitation of getcomputedstyle here
Comments
Post a Comment