Solved

for loop checking radio button

Posted on 2008-06-11
7
527 Views
Last Modified: 2009-07-29
hi all,

 i have some html with radio buttuns

I'm using a javscript for loop to loop through them all to see which one is checked.

What I am finding is if there is only ONE radio buton, even though i click it the javascript isn't recognising it's checked


for (var i = 0; i < document.forms['radio'].test.length; i++) {
  if (document.forms['radio'].test[i].checked) {
    alert ('we checked one');      
    return false;
  }                  
}

so i have one radion button on a form, clicking it so it's checked butt it's not going into the loop

thanks for any help


0
Comment
Question by:dalecon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Expert Comment

by:russhuntington
ID: 21758773
Can you post the full hmtl? It will help us idenitify the problem.
0
 

Author Comment

by:dalecon
ID: 21758889
<html>

<head>

<script language="javascript">

      function testchecked()
         {


            alert ('in fucntion');

            for (var i = 0; i < document.forms['radio'].radiotest.length; i++) {

                        alert ('in for loop');

                  if (document.forms['radio'].radiotest[i].checked) {


                        alert ('in if');

                  }

            }
         }

</script>

</head>


            <form name="find" action="" method="post">
              <td align="left">
                        <input name="Submit" type="button" class="button" value="click" onClick="javascript:testchecked()">
                  </td>
            </form>



<table width="100%" border="0" cellpadding="1" cellspacing="0">
<form name="radio">
<tr>
  <td width="20%">aaa</td>
  <td width="15%">bbb</td>
  <td width="20%">ccc</td>
  <td width="40%" align="left" ><input type="radio" name="radiotest" value="1234"></td>
</tr>
</form>
</table>


</html>
0
 

Author Comment

by:dalecon
ID: 21759084
if i do alert( document.forms['radio'].radiotest.length);

it comes up with undefined, even though there is a radiotest
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 41

Accepted Solution

by:
HonorGod earned 50 total points
ID: 21759340
Try something like this:
<html>
<head>
<script type='text/javascript'>
  function testchecked() {
//  alert ('testchecked() : len = ' + document.forms[ 1 ].radiotest.length );
    var radiotest = document.forms[ 1 ].radiotest;
    var picked = null;
    for (var i = 0; i < radiotest.length; i++) {
      if ( radiotest[ i ].checked ) {
        picked = radiotest[ i ];
      }
    }
    if ( picked ) {
      alert( 'checked = ' + picked.value );
    } else {
      alert( 'Please select a value' );
    }
  }
</script>
 
</head>
<body>
  <form name='find' action='' method='post'>
    <input name='Submit' type='button' class='button' value='click' onClick='javascript:testchecked()'>
  </form>
 
  <form name='radio'>
    <table width='100%' border='0' cellpadding='1' cellspacing='0'>
      <tbody>
        <tr>
          <td >aaa</td>
          <td >bbb</td>
          <td >ccc</td>
          <td  align='left' ><input type='radio' name='radiotest' value='1234'></td>
        </tr>
        <tr>
          <td >ddd</td>
          <td >eee</td>
          <td >fff</td>
          <td  align='left' ><input type='radio' name='radiotest' value='2345'></td>
        </tr>
      </tbody>
    </table>
  </form>
</body>
</html>

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 21759362
btw, when you only have one matching value, the result of the reference is not an array.  So with only one "radiotest" input field, this

document.forms[ 1 ].radiotest

references the one and only matching item in the document
0
 
LVL 1

Expert Comment

by:SasDev
ID: 32962244
I've been looking for a solution to this problem of mine and was able to attain a resolution with the help of the "Accepted Solution" in this thread.

Thanks 'HonorGod', your solution helped me!
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 32962423
SasDev:  Good.  I'm glad to hear it.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question