SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Egypt
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    uncheck radio buttons

    Hello,

    I have a form in which I retrieve product categories from a database and under each category there are sub categories, then I loop through the resulted recordset and create group of radio buttons to each group of subcategories

    for example:
    First Category name: Unique and Selective Choices
    Subs: 1 - Unique and selective Choices
    Name of radio group: rad_SCat2

    Second Category name: Art Work
    Subs: 1 - Souvenir
    Subs: 2 - Alaa El Din
    Subs: 3 - Miniature
    Subs: 4 - Other decorative art
    Name of radio group: rad_SCat3

    Third Category name: Boxes
    Subs: 1 - Boxes
    Name of radio group: rad_SCat4

    Fourth Category name: Miniature
    Subs: 1 - Miniature
    Name of radio group: rad_SCat5

    Fifth Category name: Furniture
    Subs: 1 - Antiques Furniture
    Subs: 1 - Desk Accessories
    Subs: 1 - Old Estate Items
    Name of radio group: rad_SCat6

    NOTE:
    All groups of radio buttons have common part in their names which is 'rad_SCat' + the group number

    The asp code for the above is:
    i.e: the asp code for retrieving categories from database and looping through the recordset to create radio buttons
    -------------------------
    If Catrs.eof = 0 Then '---if the recordset is full
    Do while not Catrs.eof '---loop through the recordset
    If Not CategoryId = Catrs("catid") Then
    CategoriesNo = Cint(CategoriesNo) + 1
    CategoryId = Catrs("catid")
    Response.write "<tr><td Colspan = '2'>"
    Response.write "<img src=''>"&Catrs("Catname")&"<br>" '---type category name
    Response.write "</td></tr>"
    End If
    Response.write "<tr><td Width = '10%'>&nbsp;&nbsp;</td>"
    Response.write "<td>"
    SCatName = Catrs("SCatname")
    SCD = Catrs("SCatId")
    %> '---the following is to create the radiobuttons
    <input type = 'radio' name = '<%="rad_SCat"&CategoriesNo%>' value='<%=Catrs("SCatId")%>' >
    <%=SCatName%>
    <%
    Response.write "</td></tr>"
    Catrs.movenext
    Loop
    ---------------------

    QUESTION:

    suppose the user checked some buttons, I need to give the user the utility to uncheck all the radio buttons he previously checked. I tried to do it with the following function but it did not work so if anyone can help me fixing this function or making a new function

    function uncheckAll() {
    for (var j = 1; j <= 9; j++) {
    box = eval("document.Frm_Product.rad_SCat" + j);
    if (box.checked == true) box.checked = false;
    }
    }

    and I call this function by clicking on an uncheck button as:
    <input type=button value="Uncheck All" onClick="uncheckAll()">

    ADDITIONAL REQUIRMENT:

    when I retrive categories from database, I do not know exactly the number of the resulting categories and so
    I do not know how many radio buttons will be created, thus I need to pass this number to the javascript function
    which will loop through all radio buttons to uncheck them.

    I hope I have explained my problem well.
    Thanks a lot

  2. #2
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about this example:
    Code:
    <html>
    <head>
    <title>Check All</title>
    <SCRIPT LANGUAGE="JavaScript">
       function Uncheckall(formid)
        {
         frm_len=formid.length  // get the number of form elements
         for(i=0;i<frm_len;i++)
             {
              if(formid.elements[i].type=="radio")
                {formid.elements[i].checked=false}
             }
       }
    </SCRIPT>
    </head>
    <body>
      <CENTER>
      <FORM NAME="myform">
        <INPUT TYPE="radio" NAME="ckb1"> Checkbox One
        <BR>
        <INPUT TYPE="radio" NAME="ckb2"> Checkbox Two
        <BR>
        <INPUT TYPE="radio" NAME="ckb3"> Checkbox Three
        <BR>
        <INPUT TYPE="button" value="Un-Check All" NAME="chka" onClick="Uncheckall(this.form)">
      </FORM>
      </CENTER>
    </body>
    </html>
    I had to edit the code because my if statement was incorrect. It should work now.
    Last edited by requestcode; Feb 18, 2005 at 07:49.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Egypt
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello requestcode,

    Thanks to you for your help, your code has worked fine, I changed it to suit my situation as I have another set of radio buttons that do not relate to the categories radio buttons which I do not want them to be unchecked.

    Code:
     <SCRIPT LANGUAGE="JavaScript">
       function Uncheckall(formid)
        {
         frm_len=formid.length  // get the number of form elements
         for(i=0;i<frm_len;i++)
             {
              if(formid.elements[i].type=="radio" && formid.elements[i].name!="rad_PType")
                {formid.elements[i].checked=false}
             }
       }
    </SCRIPT>
    Thanks a lot


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
  •