can anyone help me with some simple javascript please?

This is probably a very simple thing i'm asking for help with, but (shamefully?) i've never got around to properly learning javascript - never really found myself needing/wanting to use it that much! Anyway, I've been asked to do something in javascript and i really have no idea. Any help would be hugely appreciated.

The basic task is that i have a number of checkboxes on a page (not a set number - anywhere between one and four) and i need them to essentially behave like a radio group; only one can be selected at any one time. I guess I need some function which, upon clicking on one checkbox, automatically finds all others on the page and unselects them.

I'm clueless with this stuff, and google is proving fruitless. Anyone?

Parameter 1 - use true to check, use false to uncheck
Parameter 2 - the name of your form (i.e. in form tag, whatever "name=" is set)
Parameter 3 - the name of the form field used for your check boxes (if using PHP append [] on the end in both the form and this parameter)

Obviously these are examples, edit accordingly to use your form names and so on. Enjoy, and of course a quick note to angelwatt - this is just the way I do it, there are a million ways, no disrespect intended.

Obviously these are examples, edit accordingly to use your form names and so on. Enjoy, and of course a quick note to angelwatt - this is just the way I do it, there are a million ways, no disrespect intended.

Click to expand...

None taken. I like my way because it makes the JavaScript unobtrusive and doesn't require editing the HTML. Using href="javascript:..." and having to add JavaScript to each checkbox input in the HTML is less appealing for me. But your way does show how things can be done differently, which is one of the reason I like doing web development, the flexibility. I do hate using the onload so often, but it's not too big of a deal and can be done so it doesn't override any other use of onload from other JavaScript libraries.

Angelwatt's method is what you asked for exactly, as an alternative my method (toggling all) is used commonly on the Internet, such as in this forum software, actually! Just click on QuickLinks then Subscribed Threads to see it in action here.

Users expect to be able to check one or more checkboxes, which is their purpose compared to radio buttons which involve a single choice. So using a toggle all approach is a more user friendly approach in my opinion. If you need to do it your way, that's cool, use angelwatt's code.

Note to angelwatt:

My method does not "add JS to each checkbox" - kindly read it again, I simply provided two methods of toggling - 1) adding text links or 2) adding a single checkbox to the top of the OP's original code. No code is changed in the OP's input tags for each checkbox.

This is a reply just to clarify matters a bit, and expand a little on why I posted this alternative approach. Cheers to all.

My method does not "add JS to each checkbox" - kindly read it again, I simply provided two methods of toggling - 1) adding text links or 2) adding a single checkbox to the top of the OP's original code. No code is changed in the OP's input tags for each checkbox.

Click to expand...

Well, your methods involved adding JavaScript to a checkbox, or to add a link with JavaScript. My comment was trying to get at that I didn't like adding the JavaScript directly into the content area, and my method worked without editing any HTML in the content area (content area = inside body tag). It's not like I said your way was bad/wrong, I was simply adding rationale for the way I coded my solution. I did overlook that one of your methods was only adding JavaScript for a single checkbox though, so oops on that.

MacRumors attracts a broad audience
of both consumers and professionals interested in
the latest technologies and products. We also boast an active community focused on
purchasing decisions and technical aspects of the iPhone, iPod, iPad, and Mac platforms.