SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: radio button

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    radio button

    Why the below radio button does give the alert(...) when running it? what's wrong.
    Code:
    <script type="text/javascript">
    for (i=0;i<document.forms.radios.length;i++) 
    {
    	if (document.forms.radios[i].checked==true) {
    		alert(document.forms.radios[i].value);
    	}
    }
    </script>
    <form name="forms" method="post" action="">
      <p>
        <label>
        <input type="radio" name="radios" value="a" checked>
        A</label>
        <br>
        <label>
        <input type="radio" name="radios" value="b">
        B</label>
        <br>
      </p>
    </form>
    <p>&nbsp;</p>
    </body>
    </html>
    Last edited by stymiee; Jul 16, 2007 at 08:35. Reason: added code tags

  2. #2
    SitePoint Wizard rbutler's Avatar
    Join Date
    Jul 2003
    Location
    Springfield, MO
    Posts
    1,867
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because you're looping through an array set and you first radio button is set to checked. Therefore, the first element in the array is set to checked by default, thus, the alert is showing.
    Ryan Butler

    Midwest Web Design


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
  •