View Full Version : getting the value of a checkbox like var[id]
firepages
10-03-2002, 12:16 PM
Hi, any ideas how I can check via javascript if these checkboxes have been checked or not?
<input name="Oct31[adult]" type="checkbox" value="1" onclick="addit();">
<input name="Nov1[adult]" type="checkbox" value="1" onclick="addit();">
<input name="Nov2[adult]" type="checkbox" value="1" onclick="addit();">
<input name="Nov3[adult]" type="checkbox" value="1" onclick="addit();">
I know if I use "Oct31_adult" etc but I would rather not if there is a straightforward way in javascript ???
glenngv
10-03-2002, 12:31 PM
<input name="Oct31[adult]" type="checkbox" value="1" onclick="addit(this);">
function addit(objChk){
alert(objChk.checked)
}
firepages
10-03-2002, 12:43 PM
Hi glenngv , I can see where you are going... but I am not sure if I can use that in the way I need to ?
err its easier to show than explain :)
http://www.firepages.org/calculate.html
at the moment the form adds up number of tickets * price - but I need it to do that for each day as well ?
(I know the codes a mess but its PHP output)
glenngv
10-03-2002, 12:53 PM
do you mean in the addit() function, you always want to check only for Oct31[adult] checkbox? not the one whose checkbox is clicked?
firepages
10-03-2002, 01:10 PM
Hi sorry I have problems saying what I mean :)
the addit() function is just meant to recalculate the form totals whenever any of the form inputs is altered (and shoulf by default have at leat one day selected - just not done that yet)
so say someone wants 3 adult tickets @ $13 each then at the moment the form works out that $39 is the total - but thats only for one day
ideally if the user then selected 2 more dates then that would knock the total up to $117 etc
so addit does no more than recalculate all the form values , so the calculation would then be...
(Oct31[adult]+Nov1[adult]+Nov2[adult]+Nov3[adult])*(10 * document.bella1.adult.value);
but 'Oct31[adult]' appears to be meaningless to javascript ... I could do this...
document.bella1.Oct31_adult.value + etc using name="Oct31_adult" etc
but I would rather have the Oct31[adult] array format if possible ?
document.bella1.elements['Oct31[adult]'].checked
:)
firepages
10-03-2002, 04:45 PM
:D
(assuming you dont tell me its moz only :: haha)
very much appreciated cheers
RadarBob
10-04-2002, 01:05 AM
Maybe this will be helpful...
If you name all the checkboxes from one row (ie "adult" ticket row or "child", etc.) the same, then you could treat them like an array - just like radio buttons. That would make it easer to add things up and also make the javascript easier to understand I think.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.