Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Iterating through checkboxes using JavaScript

18 views
Skip to first unread message
Message has been deleted

David Mark

unread,
Oct 15, 2007, 4:24:47 AM10/15/07
to
On Oct 15, 3:57 am, Melbfella <d...@dodeonline.com> wrote:
> 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[i].checked==true){
> alert(frmMain.chkOption[i].value);
> dblCost =dblCost +
> (frmMain.cboTourOptionQty[i].value*parseInt(frmMain.txtOptionCost[i].value)­);

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

parseInt(frmMain.txtOptionCost[i].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.

Bart Van der Donck

unread,
Oct 15, 2007, 11:08:31 AM10/15/07
to
David Mark wrote:

>> function calculateCost(){
>> var dblCost=0;
>> var lSeats=frmMain.txtSeats.value;
>
> The prefixes on these variables make no sense in JavaScript.

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

http://en.wikipedia.org/wiki/Hungarian_notation

--
Bart

David Mark

unread,
Oct 15, 2007, 6:00:50 PM10/15/07
to
On Oct 15, 11:08 am, Bart Van der Donck <b...@nijlen.com> wrote:
> David Mark wrote:
> >> function calculateCost(){
> >> var dblCost=0;
> >> var lSeats=frmMain.txtSeats.value;
>
> > The prefixes on these variables make no sense in JavaScript.
>
> 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.

>
> http://en.wikipedia.org/wiki/Hungarian_notation
>

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.

Bart Van der Donck

unread,
Oct 16, 2007, 2:02:26 AM10/16/07
to
David Mark wrote:

> On Oct 15, 11:08 am, Bart Van der Donck <b...@nijlen.com> wrote:
>
>> David Mark wrote:
>>>> function calculateCost(){
>>>> var dblCost=0;
>>>> var lSeats=frmMain.txtSeats.value;
>
>>> The prefixes on these variables make no sense in JavaScript.
>
>> 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.

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.

>> http://en.wikipedia.org/wiki/Hungarian_notation
>
> 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.

--
Bart

David Mark

unread,
Oct 16, 2007, 2:12:09 AM10/16/07
to
On Oct 16, 2:02 am, Bart Van der Donck <b...@nijlen.com> wrote:
> David Mark wrote:
> > On Oct 15, 11:08 am, Bart Van der Donck <b...@nijlen.com> wrote:
>
> >> David Mark wrote:
> >>>> function calculateCost(){
> >>>> var dblCost=0;
> >>>> var lSeats=frmMain.txtSeats.value;
>
> >>> The prefixes on these variables make no sense in JavaScript.
>
> >> 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.
>
> 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.

Mike Scirocco

unread,
Oct 28, 2007, 1:52:37 AM10/28/07
to
looks like Visual Basic
0 new messages