PDA

View Full Version : Disable multiple checkboxes that have the same name but different IDs



bluemonster7754
06-21-2005, 03:25 PM
Hello Javascript gurus! I have a problem. I have a table that has three rows of six checkboxes. Each row of checkboxes has the same name, but has different IDs. For example, Row 1 names and IDs would be:

input type=checkbox value="1" id="V42_1" name="V42"
input type=checkbox value="2" id="V42_2" name="V42"
and so on...
input type=checkbox value="6" id="V42_6" name="V42"

How would I be able to disable the second checkbox if the first checkbox is checked? This would be same the second pair of checkboxes as well. I am trying to guide users in answering certain questions that are relevant to each response in a survey that I am developing. This would also prevent the user from answering the same question twice and thus giving me an inflated data set to sift through in the end while I am doing my analysis. I thought checkboxes would be a better choice than radio buttons because it would prevent users from thinking they just have to answer one question instead of answering three per line...

I was think something to the effect of referencing their IDs with an array, but I am not sure how to accomplish this. If this is not clear, feel free to reply back for detail. Please help!

Thank you in advance!

Twey
06-21-2005, 04:03 PM
Use document.getElementById("V42_1").checked.

jscheuer1
06-21-2005, 06:13 PM
Radio buttons are more suited to this sort of thing, as only one may be selected in this situation. To gain control over all checkboxes of a given name however, use:

var boxes=document.getElementsByTagName('input')
for (var i = 0; i < boxes.length; i++){
if (boxes[i].name=='v42')
do something here
}

bluemonster7754
06-21-2005, 06:14 PM
Thank you very much for the quick response.

Would the script look like:

<input type=checkbox value="1" id="V42_1" name="V42 onClick("document.getElementById("V42_2").checked")>?

Can you also have it so it can make more than one checkbox disabled as well or does that involve more scripting? Sorry, I'm a newbie to Javascript :-).

mwinter
06-21-2005, 06:41 PM
To gain control over all checkboxes of a given name however, use:

var boxes=document.getElementsByTagName('input')
for (var i = 0; i < boxes.length; i++){
if (boxes[i].name=='v42')
do something here
}That would iterate over all input elements in the document with the name, v42. For checkboxes only,


var group;

if(document.getElementsByName) {
group = document.getElementsByName('V42');

for(var i = 0, n = group.length; i < n; ++i) {
if('checkbox' == group[i].type) {
/* Act here */
}
}
}If we're just talking about checkboxes within a particular form, and we probably are, then:


var group = document.forms.formName.elements.V42;

for(var i = 0, n = group.length; i < n; ++i) {
if('checkbox' == group[i].type) {
/* Act here */
}
}


Would the script look like:

<input type=checkbox value="1" id="V42_1" name="V42 onClick("document.getElementById("V42_2").checked")>?No. It would need to be much more complicated, which is why you should go back to radio buttons - they do the work for you. If you're worried about clarity, then obviously you need to reorganise the layout. This could be achieved by placing each question on a single line/row (a very sensible thing to do), or use fieldset elements to group questions.

Using a script is not a solution, particularly as scripts can be circumvented or disabled.

Mike

bluemonster7754
06-21-2005, 07:09 PM
It looks like I should be better off reorganizing the survey layout to best suit what I need it to do with radio buttons. Thanks for everyone's help. I am sure I'll be asking more questions in the near future as they come up.

Thanks again gurus!!! :D