mardi 11 août 2015

.onchange or .onclick and checkbox value to JS script

I have an array of checkboxes group into class='year'. I would like to call a function in JS when a checkbox is checked.

The html is,

<table class="inner" id="searchTable">
                                         <search>Exam Type:<th>                        
                                                    <?php 
                                                        foreach($exams as $key=>$value):

                                                               echo "<tr><td class='left'><input type='checkbox' class='year' id='$key' name='$key' value='$value'

                                                            if ($category['selected'])
                                                            {
                                                            echo ' checked';
                                                            }

                                                            > $value</td></tr>";
                                                          endforeach; ?>
                                     </table>

This table is inside a form. but I am too worried about the form at the moment.

The JS should create a table with a row when any checkbox is checked.

function yearTable(){
    var table = document.getElementById("searchYear");
    var row = document.createElement("tr");    
    var cell = document.createElement("td");
    var empty = document.createTextNode("year.value");
    /*var empty = document.createTextNode("");*/
    cell.appendChild(empty);
    row.appendChild(cell);
    table.appendChild(row); 
}

document.getElementByClassName('year').onchange = yearTable; 

I have tried using .onchange and .clicked but neither do anything when a box is checked. Also I need the value checked in the JS. I tried year.value but that doesn't work. Prviously I has a select options menu with id = 'exam' and I was able to get the value using exam.value but I can't figure out how to do soemthing equivalent for these checkboxes. Any help would be greatly appreciated. Thanks




Aucun commentaire:

Enregistrer un commentaire