Hi again,
First off, thanks both of you for your time. I guess you know what this is like, you start playing with jquery, making show/hide a few divs and then you think you know your way around js and jquery. Truth is I keep freaking out a bit when I see any array, not because I don’t understand what they do (not a big accomplishment), but rather because I still feel unable to write them from scratch, not to talk about being creative!!
I went with Chris solution, because his array looked more familiar to me, and because the idea of simply substituting - onChange=“presupuesto()” - for - class=“change” - in each - input type=“checkbox” - looked like an easy way to handle the thing to me. I chose ‘onclick’ instead of ‘onchange’ though.
Despite the fact chris example at jsfiddle is definetely working I didn´t manage to make it work on my site.
The url is: http://www.ficoprieto.com
js external file is ‘presupuesto.js’
Sorry I haven’t got it translated into english yet. I’ll have it done soon.
Click on the euro symbol (€) top-right of the page. You’ll see no changes in <inputs> from the code I wrote on my 1st post. The reason for that is that the calculator is actually working (everywhere but IE8 - I’m not trying below version 8 - which shows a weird ‘behaviour’ for that form), but it does it with the obtrusive onchanges. I tested Chris solution locally.
My aim was to build up a calculator so users could be adding functionalities to their future website. Each functionality a checbkox, each checkbox a different price to be added (or substracted if the checkbox is deselected) to the ‘basic website’ price of 330€
You’ll see too that de id’s for the checkboxes go from ‘ch1’ to ‘ch7’ instead of ‘show’, ‘ilikeit’, ‘gallery’… this is because I was trying to add a “tick’em all” checkbox running an array through all checkboxes, which I didn’t managed to do either. But that’s a different matter.
Here is the html and js I’m keeping locally after applying Chris solution:
HTML
<input type="checkbox" name="show" value="" id="ch1" class="change" />
<input type="checkbox" name="ilikeit" value="" id="ch2" class="change" />
<input type="checkbox" name="gallery" value="" id="ch3" class="change" />
<input type="checkbox" name="contact" value="" id="ch4" class="change" />
<input type="checkbox" name="news" value="" id="ch5" class="change" />
<input type="checkbox" name="maps" value="" id="ch6" class="change" />
<input type="checkbox" name="llegar" value="" id="ch7" class="change" />
and here is the external js file ‘presupuesto.js’
function presupuesto() {
var total = 330;
//checkboxes
if (document.getElementById("ch1").checked){ // slideshow
var vshow = 45;}
else
{var vshow = 0;}
if (document.getElementById("ch2").checked){ // Ilikeit
var vilikeit = 15;}
else
{var vilikeit = 0;}
if (document.getElementById("ch3").checked){ // gallery lightbox
var vgallery = 20;}
else
{var vgallery = 0;}
if (document.getElementById("ch4").checked){ // contact php form
var vcontact = 40;}
else
{var vcontact = 0;}
if (document.getElementById("ch5").checked){ // newsletter php form
var vnews = 20;}
else
{var vnews = 0;}
if (document.getElementById("ch6").checked){ // google maps
var vmaps = 20;}
else
{var vmaps = 0;}
if (document.getElementById("ch7").checked){ // google maps 'cómo llegar'
var vllegar = 45;}
else
{var vllegar = 0;}
var total_check = vshow + vilikeit + vgallery + vcontact + vnews + vmaps + vllegar;
// calculo del total
document.getElementById("resultado").value = parseInt(total) + parseInt(total_check) + ' euros';
}
var ckbx = document.getElementsByTagName('input');
if (ckbx.length) {
for (var i = 0, m = ckbx.length; i < m; i++) {
if (ckbx[i].type === 'checkbox' && ckbx[i].className.match(/change/)) {
ckbx[i].onclick = presupuesto;
}
}
}
Any idea why it wouldn’t work?
Thanks a lot again,
Whitecreek.