Iterating through checkboxes using JavaScript

M

Melbfella

G'day all

I apologise in advance for the newbiness of this question, but the
answer has eluded me for 3 days now - hopefully one of you gurus out
there might be able to help .......

I have a page that dynamically draws checkboxes and am attempting to
use the following code to iterate through each text box, see if it's
checked - if checked, change a combo box to match the number typed in
a checkbox. Here's the code that calls the functions, and the
functions themselves......

**Code that fires the event
<input class="ctlstandard" type="text" name="txtSeats" id="txtSeats"
onblur="calculateCost();setOptionCombos();"/>&nbsp;<font color="<
%=session("BrandTextColour")%>">*</font></td>

<input type="checkbox"
onclick="showHideJumpPoints(this);calculateCost();" name="chkOption"
value="<%=rs("TourOptionID")%>"/>


**The functions
function calculateCost(){

var dblCost=0;
var lSeats=frmMain.txtSeats.value;
//alert('calc Cost');
if(isNaN(lSeats)){
document.all.txtSeats.focus;
alert('Please enter a number in the seats field');
}
dblCost=lSeats*frmMain.txtTourCost.value;

for (i=0;i<frmMain.chkOption.length;i++){
if (frmMain.chkOption.checked==true){
alert(frmMain.chkOption.value);
dblCost =dblCost +
(frmMain.cboTourOptionQty.value*parseInt(frmMain.txtOptionCost.value));
//alert('This option is checked');
//break //exist for loop, as target acquired.
}
}
document.getElementById('TotalCost').innerHTML='<b>$' +
CurrencyFormatted(dblCost) + '</b>';
}


function setOptionCombos(){
var lSeats=frmMain.txtSeats.value;
for (i=0;i<frmMain.cboTourOptionQty.length;i++){
frmMain.cboTourOptionQty.selectedIndex=lSeats-1;
}
}

This works if there are two checkboxes on the screen, but does not
when there's only one.

Any help or thoughts would be greatly appreciated :)

Cheers,

Doug.
 
D

David Mark

G'day all

I apologise in advance for the newbiness of this question, but the
answer has eluded me for 3 days now - hopefully one of you gurus out
there might be able to help .......

I have a page that dynamically draws checkboxes and am attempting to
use the following code to iterate through each text box, see if it's

I assume you mean iterate through the checkboxes.
checked - if checked, change a combo box to match the number typed in
a checkbox. Here's the code that calls the functions, and the
functions themselves......

**Code that fires the event
<input class="ctlstandard" type="text" name="txtSeats" id="txtSeats"
onblur="calculateCost();setOptionCombos();"/>&nbsp;<font color="<
%=session("BrandTextColour")%>">*</font></td>

Don't post server side code.
<input type="checkbox"
onclick="showHideJumpPoints(this);calculateCost();" name="chkOption"
value="<%=rs("TourOptionID")%>"/>

**The functions
function calculateCost(){

var dblCost=0;
var lSeats=frmMain.txtSeats.value;

The prefixes on these variables make no sense in JavaScript.
//alert('calc Cost');
if(isNaN(lSeats)){
document.all.txtSeats.focus;

The all object is proprietary. Use document.getElementById. Or,
assuming this input is in a form element, use the elements property of
the form object.
alert('Please enter a number in the seats field');
}

You didn't return after the error alert.
dblCost=lSeats*frmMain.txtTourCost.value;

It seems you are testing in IE. Use this syntax to ensure
compatibility with other browsers:

document.forms.frmMain.elements.txtTourCost.value
for (i=0;i<frmMain.chkOption.length;i++){

Here is your problem. If there is only one checkbox, then chkOption
(document.forms.frmMain.elements.chkOption) will return an element
object, which has no length property or item method.
if (frmMain.chkOption.checked==true){
alert(frmMain.chkOption.value);
dblCost =dblCost +
(frmMain.cboTourOptionQty.value*parseInt(frmMain.txtOptionCost.value)­);


Always specify a base for parseInt. Assuming you expect decimal
input, the syntax is:

parseInt(frmMain.txtOptionCost.value, 10)
//alert('This option is checked');
//break //exist for loop, as target acquired.

Are these checkboxes supposed to be mutually exclusive? If so, use
radio buttons.
}
}
document.getElementById('TotalCost').innerHTML='<b>$' +
CurrencyFormatted(dblCost) + '</b>';

Better to use a read-only text input and set its value. That way your
script won't require document.getElementById or the proprietary
innerHTML property.
 
D

David Mark

I beg to disagree. I think the code can only become more
transparent.

I'm not sure what that means. It seems to me that the prefixes make
no sense in JavaScript, but make perfect sense in other languages.

I know what the prefixes are supposed to denote, but still don't think
the above examples make sense in JavaScript. These would make sense
though:

bMyFlag, strMyString, objMyObject, etc.
 
B

Bart Van der Donck

David said:
I'm not sure what that means. It seems to me that the prefixes make
no sense in JavaScript, but make perfect sense in other languages.

In the code of the original poster, "frmMain" seems to refer to the
main form on the page, "chkOption" to an array of checkboxes, etc. I
don't think Hungarian Notation depends on the language; it's about
personal taste and habit - X feels comfortable with it, Y doesn't.
I know what the prefixes are supposed to denote, but still don't think
the above examples make sense in JavaScript. These would make sense
though:

bMyFlag, strMyString, objMyObject, etc.

Actually I think it's just a coding style preference; encouraged by
some, discouraged by others.
 
D

David Mark

In the code of the original poster, "frmMain" seems to refer to the
main form on the page, "chkOption" to an array of checkboxes, etc. I

I agree with those are useful. It was the "l" and "dbl" prefixes that
seened more suited to another language.
don't think Hungarian Notation depends on the language; it's about
personal taste and habit - X feels comfortable with it, Y doesn't.

I have nothing against Hungarian Notation in general.
 
M

Mike Scirocco

David said:
I agree with those are useful. It was the "l" and "dbl" prefixes that
seened more suited to another language.


I have nothing against Hungarian Notation in general.
looks like Visual Basic
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top