reset radiobuttonlist in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satyasree
    New Member
    • Jul 2007
    • 2

    reset radiobuttonlist in javascript

    how do i reset radiobuttonlist in javascript

    i need to reset radiobuttonlist on click of a hyperlink
    i have written the code as
    document.getEle mentById('rblOf fshoreFamiliari ty').selectedIn dex=0;
    document.getEle mentById('rblOf fshoreFamiliari ty').value=1;
    document.getEle mentById('rblOf fshoreFamiliari ty').value=opti ons[-1];

    none of this is working

    can u guyz help me out with this
    Last edited by satyasree; Jul 19 '07, 07:21 AM. Reason: not in detail
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Do you only want to reset the radio buttons or everything on the form? If you don't mind resetting everything, just use the form's reset() method.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #3
      hi ...

      in case you want only the radios resetted have a look at the following example:

      [HTML]<html>
      <head>
      <script>
      function reset_radio_grp () {
      var rgrp = document.getEle mentsByName('rb lOffshoreFamili arity');
      rgrp[0].checked = 'checked';
      }
      </script>
      </head>
      <body>
      <form name="my_form">
      <input type="radio" name="rblOffsho reFamiliarity" value="1" checked="checke d"/>
      <input type="radio" name="rblOffsho reFamiliarity" value="2"/>
      <input type="radio" name="rblOffsho reFamiliarity" value="3"/>
      <input type="button" value="reset radio group" onclick="reset_ radio_grp();"/>
      </form>
      </body>
      </html>
      [/HTML]

      kind regards

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by gits
        hi ...

        in case you want only the radios resetted have a look at the following example:
        Yes, I thought I might as well take the simple approach first.

        However, isn't a set of radio buttons normally unchecked?

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          hi ...

          at the w3c-html-spec you find the following in the radio-buttons section:

          At all times, exactly one of the radio buttons in a set is checked. If none of the <INPUT> elements of a set of radio buttons specifies `CHECKED', then the user agent must check the first radio button of the set initially.
          Since user agent behavior differs, authors should ensure that in each set of radio buttons that one is initially "on".
          so i think one radio-button should be checked initially ... since FF is not doing we have to specify it for ourselves ... in case i get what the spec says? ... i think this is to do, since the radios require a decision between its options ... and so one is to be made in every case? otherwise we should use checkboxes ... i think?

          kind regards

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by gits
            so i think one radio-button should be checked initially ... since FF is not doing we have to specify it for ourselves ... in case i get what the spec says? ... i think this is to do, since the radios require a decision between its options ... and so one is to be made in every case? otherwise we should use checkboxes ... i think?

            kind regards
            Thanks for the clarification. You are right about the purpose of a radio button, but many times users don't bother setting default values for radio buttons.

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              ;) yes ... in that case we should have a function that gets the initial checked-state of the radios at the onload of the body ... we store that state and on reset we restore it ...

              and it seems that FF doesn't follow the specification in that case? it should initially check the first radio when the 'programer' didn't ... does other browsers do? ... i don't know because i generally try to avoid radios ... i don't find them very useful ... in most cases its better replaced with a dropdown-box

              kind regards

              Comment

              • davidj
                New Member
                • Aug 2007
                • 6

                #8
                Hi,

                Tried this and it just resets to the first value.

                How do you unset all the radio button choices.

                Thanks
                David J
                Bytes Interactive
                http://www.bytesintera ctive.com

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by davidj
                  Hi,

                  Tried this and it just resets to the first value.

                  How do you unset all the radio button choices.
                  David, welcome to TSDN!

                  Loop through the radio buttons and set the checked value to false:
                  [CODE=javascript]for (i = 0; i < rgrp.length; i++) {
                  rgrp[i].checked = false;
                  }[/CODE]Don't forget though that radio buttons are supposed to have an initial state.

                  Comment

                  • davidj
                    New Member
                    • Aug 2007
                    • 6

                    #10
                    Thanks
                    That worked.

                    dj

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Originally posted by davidj
                      Thanks
                      That worked.

                      dj
                      You're welcome.

                      Comment

                      Working...