Results 1 to 6 of 6

Thread: Disable multiple checkboxes that have the same name but different IDs

  1. #1
    Join Date
    Jun 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Disable multiple checkboxes that have the same name but different IDs

    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!

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Use document.getElementById("V42_1").checked.
    Last edited by Twey; 06-21-2005 at 04:06 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Jun 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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 :-).

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1
    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,

    Code:
    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:

    Code:
    var group = document.forms.formName.elements.V42;
    
    for(var i = 0, n = group.length; i < n; ++i) {
      if('checkbox' == group[i].type) {
        /* Act here */
      }
    }
    Quote Originally Posted by bluemonster7754
    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

  6. #6
    Join Date
    Jun 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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!!!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •