Select Radio By Clicking Table Row

This code will set an event on a table row, so that when the row is clicked, the radio button is select.  It is used to easily select a radio button associated with the row.  jQuery can also do this with a smaller coding foot print.  This is just an example of how you might provide this functionality with JavaScript.

//************************************************************************************************************* //
//
// Place this code in the module you want to provide clickRow capability . Enter the table id and radio button name.
//
//

clickRow(document.getElementById(‘table_id’), ‘radio_name’);

//************************************************************************************************************* //
//
// This function causes a radio button to be checked when the row is selected. Should be used in conjunction
// with function clearRadios().
//

function clickRow(table, radioname) {
   var tablerows = table.getElementsByTagName(‘tr’);

    function selectRow(r, radioname) {
       var len = document.getElementsByName(radioname).length;
        x = r.rowIndex;
        ri = -1;

        if(len > 1) {
           for(z=1; z<=x; z++) {
                 var cell = tablerows[z].getElementsByTagName(‘td’)[0];
                 if(cell.innerHTML.indexOf(‘<input’) > -1 || cell.innerHTML.indexOf(‘<INPUT’) > -1) {
                     ri = ri + 1;
                 }
            }
            if(!document.getElementsByName(radioname)[ri].disabled) {
                clearRadios();
                document.getElementsByName(radioname)[ri].checked = true;
            }
        } else {
            if(!document.getElementsByName(radioname)[0].disabled) {
                clearRadios();
                document.getElementsByName(radioname)[0].checked = true;
            }
        }

    }

    for (i=0; i < tablerows.length; i++) {
         tablerows[i].onclick = function() {
             selectRow(this, radioname);
         }
   }
}

//************************************************************************************************************* //
//
// This function clears and disables all radio buttons on a screen.
//

function clearDisableRadios() {
     var inputs = document.getElementsByTagName(‘input’);
     for(var i=0; i<inputs.length; i++){
         if(inputs[i].getAttribute(‘type’)==’radio’){
             inputs[i].checked = false;
             inputs[i].disabled = true;
         }
     }
}

//************************************************************************************************************* //
//
// This function clears all radio buttons on a screen.
//

function clearRadios() {
     var inputs = document.getElementsByTagName(‘input’);
     for(var i=0; i<inputs.length; i++){
         if(inputs[i].getAttribute(‘type’)==’radio’){
             inputs[i].checked = false;
         }
     }
}

//************************************************************************************************************* //
//
// This function simply counts the number of rows in a table.
//

function countRows(table) {
    var len = table.getElementsByTagName(‘tr’).length;
    return len;

}