Results 1 to 4 of 4

Thread: How to check dynamically created radio buttons

  1. #1
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to check dynamically created radio buttons

    I have several sets of dynamically created radio buttons. Each with a yes,no, and n/a.

    Code:
    <input type="Radio" name="r_1_237" id="r1_1_237" value="yes">Yes<br />
    <input type="Radio" name="r_1_237" id="r2_1_237" value="no">No<br />
    <input type="Radio" name="r_1_237" id="r3_1_237" value="na">N/A
    
    <input type="Radio" name="r_1_238" id="r1_1_238" value="yes">Yes<br />
    <input type="Radio" name="r_1_238" id="r2_1_238" value="no">No<br />
    <input type="Radio" name="r_1_238" id="r3_1_238" value="na">N/A
    
    <input type="Radio" name="r_2_239" id="r1_2_239" value="yes">Yes<br />
    <input type="Radio" name="r_2_239" id="r2_2_239" value="no">No<br />
    <input type="Radio" name="r_2_239" id="r3_2_239" value="na">N/A
    The id of the NA radio buttons always start with 'r3_' and the value is always = 'na'.

    I need a 'Check All N/A' button that would select all the N/A radio buttons in all the sets of buttons. I am not sure how to reference the buttons if I don't know the exact id.

    Thanks
    Last edited by foneguy2; 03-27-2009 at 07:28 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Try this:
    Code:
    <script type="text/javascript">
    var checkAll = function(begins){
      for(i=0; (a=document.getElementsByTagName('input')[i]); i++){
        if(a.type == "radio"){
          var id = a.id.substring(0, begins.length);
          if(id == begins){
            a.checked = true;
          }
        }
      }
    }
    </script>
    <a href="javascript:void(0)" onclick="checkAll('r3_');">Check all N/a</a><br />
    <input type="Radio" name="r_1_237" id="r1_1_237" value="yes">Yes<br />
    <input type="Radio" name="r_1_237" id="r2_1_237" value="no">No<br />
    <input type="Radio" name="r_1_237" id="r3_1_237" value="na">N/A
    
    <input type="Radio" name="r_1_238" id="r1_1_238" value="yes">Yes<br />
    <input type="Radio" name="r_1_238" id="r2_1_238" value="no">No<br />
    <input type="Radio" name="r_1_238" id="r3_1_238" value="na">N/A
    
    <input type="Radio" name="r_2_239" id="r1_2_239" value="yes">Yes<br />
    <input type="Radio" name="r_2_239" id="r2_2_239" value="no">No<br />
    <input type="Radio" name="r_2_239" id="r3_2_239" value="na">N/A
    You can also do this for the other checkboxes, I made this so when you call the function, you type in what the id begins with, and then it finds all of those and checks them.
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    foneguy2 (03-27-2009)

  4. #3
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Perfect! Worked like a charm.
    THANKS for the quick response too.

  5. #4
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Glad to help you! Your welcome!

    It seems your topic is solved... Please set the status to resolved.. To do this:
    Go to your first post ->
    Edit your first post ->
    Click "Go Advanced" ->
    Then in the drop down next to the title, select "RESOLVED"
    Jeremy | jfein.net

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
  •