Javascript Reference Guide

 
 
This guide contains useful Javascript tips.







Radio button.



Information
none

Code
    <script type="text/javascript" language="JavaScript">
    function selectItem() {
       selectedItem=document.rbForm.selectedItem;
       // This if statment is needed if no items are displayed
       if (selectedItem) {
           // This if statement is needed if more than one item is displayed.
           if (selectedItem.length) {
               for (i = 0; i<selectedItem.length; i++) {
                   if (selectedItem[i].checked) {
                       if (selectedItem[i].value > 0) {
                           alert("More than one item: 
                           selectedItem[i].value = " + 
                           selectedItem[i].value);
                       }
                   }
               }
           } else if (selectedItem.checked) {
               // If statement is needed if only one item is displayed
               alert("Only one item: selectedItem.value = " +
               selectedItem.value);
           }
       }
    }
    </script>

    <form name="rbForm">
        <table border="1">
        <tr>
            <td>Id</td>
            <td>Name</td>
        </tr>
        <tr>
            <td><input type="radio" name="selectedItem" value="1" /></td>
            <td>John</td>
        </tr>
        <tr>
            <td><input type="radio" name="selectedItem" value="2" /></td>
            <td>Jim</td>
        </tr>
        <tr>
            <td><input type="radio" name="selectedItem" value="3" /></td>
            <td>Jones</td>
        </tr>
        </table>
        <p>
            <input type="button" onfocus="this.blur()" value="select" 
            onclick="javascript:selectItem();return false;" />
        </p>
    </form>
	
Result

Id Name
John
Jim
Jones