getting value of a radio button....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahullko05
    New Member
    • Oct 2008
    • 34

    getting value of a radio button....

    how can we get the value of a checked radio button .?
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function checkThis(ths)
    {
       if(ths.checked == true)
       {
          alert("You selected "+ths.value);
       }
    }
    </script>
    </head>
    <body>
      <input type="radio" name="sex" value="male" onclick="checkThis(this)"/> Male<br />
      <input type="radio" name="sex" value="female" onclick="checkThis(this)"/> Female<br />
    </body>
    </html>
    This is one way of retrieving the value of radio.. There are numerous ways.. Hope this will be useful for you...

    Regards
    Ramanan Kalirajan

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      or using "this"
      Code:
      <html>
      <head>
      <script type="text/javascript">
      function checkThis()
      {
         if (this.checked == true)
         {
            alert("You selected "+this.value);
         }
      }
      
      attach()
      {
          var inp = document.getElementsByName("sex");
          inp[0].addEventListener("click", checkThis, false);
          inp[1].addEventListener("click", checkThis, false);
      }
      
      window.addEventListener("load", attach, false);
      /* important note
      IE doesn't understand these Event handlers, 
      use addEvent() (see google) for cross browser support
      */
      </script>
      </head>
      <body>
        <input type="radio" name="sex" value="male"/> Male<br />
        <input type="radio" name="sex" value="female"/> Female<br />
      </body>
      </html>

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        You have used addEevntListene r and attachEvent.
        But what's the difference between ..
        Code:
        obj_ref.onEventName = function_ref;
        and

        Code:
        /**
        using proper function call
        **/

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Originally posted by dmjpro
          But what's the difference between ..
          what do you mean by that?

          Comment

          • rahullko05
            New Member
            • Oct 2008
            • 34

            #6
            hey it really works...thank you so much.

            Comment

            • gopan
              New Member
              • Apr 2009
              • 41

              #7
              Originally posted by dmjpro
              You have used addEevntListene r and attachEvent.
              But what's the difference between ..
              Its a commented area dmjpro,
              Both the addEventListene r and attachEvent are functionally same.

              attachEvent is the IE equivalent of addEventListene r

              hope you got the grip...

              Comment

              Working...