logo

Fun With CheckBoxes

Boy am I have fun with these CheckBoxes. Whether or not we unearthed a bug yesterday (I think most of us agree it was) it goes without saying that CheckBoxes are strange animals. If you use the DOM to access them in JavaScript you need to account for how many there are. If it's a lone checkbox the syntax is:

alert( document.forms[0].CheckBoxField.checked );

If it's the first of many then it now belongs to an array of checkboxes all with the same name and you need to take this in to account like so:

alert( document.forms[0].CheckBoxField[0].checked );

This is the case when you have just one checkbox and you're using the bug fix of adding a hidden text field straight after any checkbox. The single checkbox is then a part of an array of two objects with the same name and is accessed using the same syntax as above.

Stilll on checkboxes, one of my tasks today was to make an interactive one. When you clicked this checkbox it would un-grey-out a read-only text field depending on whether the checkbox was checked or not. In my obsession with minimising the length of JavaScript event code I came up with the following onclick event:

if (this.checked){
with(this.form.TextFieldName){readOnly=false;style.backgroundColor='white'}
}else{
with(this.form.TextFieldName){value='';readOnly=true;style.backgroundColor='#f1f1f1'}
};

Notice the way I accessed the checkbox and its form using the special "this" operator and that I worked on the text field using the "with" statement. Both of which saved me a lot of space and time. Worth your remembering!

Still on JavaScript, Maarten Visser sent in a JavaScript Object browser he's written. Get to know your way around the DOM and you'll start to find all those mysterious scripts starting making a whole lot more sense.

Thanks to Andy Davies for the link to VisualRoute which you can use to see the route your data takes around the globe (roughly of course). Here's where these words are about to go when I press the Submit button in a second or two....

VisualRoute

For the those with Geographic Illiteracy it goes from the UK (the smaller country) to the US (the bigger country) passing over the Atlantic Ocean.

And finally, thanks to Tone Walters for his wishlist gift. Appreciated!

Comments

    • avatar
    • Chris Melikian
    • Fri 29 Nov 2002 10:41

    Instead of having a hidden field for each checkbox, you could have one field with a comma-separated list of checkboxes.

    The onSubmit event could then loop through each of the field names using field.value.split(',') and add a text field using the DOM for each check box as appropriate before the form is submitted to the server.

    This would save code space and be generic enough for use in other forms?

    Cheers, Chris.

    • avatar
    • Jake
    • Fri 29 Nov 2002 10:55

    There are many wills to kill a cat Chris but there's no need to do it in such a cold-blooded fashion ;o)

    • avatar
    • Chris Melikian
    • Fri 29 Nov 2002 11:12

    Chris kill cat.

    Chris tired.

    Chris need sleep.

    Chris go home now and rebuild life.

    ;-))

    • avatar
    • tq
    • Fri 29 Nov 2002 11:14

    Jake, I think VisualRoute misses out one vital node - Bermuda! BDA is the hop point for most of the UK-US data transmissions.

    tq

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Fri 29 Nov 2002

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content