Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding select/deselect all to dynamic tabular form

I have a form which contains buttons to add and delete rows. My javascript function to check all checkboxes works for the first row, but once I add more rows to the form, the first row is still the only one that gets checked.

Any tips?

Here is my javascript function:

<code>
//checks all rows
function checkAll() {
var masterCheck = document.getElementById('masterCheck');
var on = false;
if(masterCheck.checked==true) {
    document.getElementById('checkbox').checked=true;
} else {
    document.getElementById('checkbox').checked=false;
}
}
</code>

And here is the form: http://crimsonroot.com/files/freelance/new.html

Any help is appreciated!

like image 332
Gabriel Avatar asked Mar 13 '26 20:03

Gabriel


1 Answers

I found out what was wrong! @Mohammed your answer really helped. There were just one or two syntax errors that I found. In order to check and uncheck all of the boxes, I needed to add a boolean variable as an input to the function as follows:

//checks all rows
function checkAll(bool) {
    var masterCheck = document.getElementById('masterCheck');
    var allcheck = document.getElementsByClassName('checkbox');
    var on = false;
    for (var i = 0; i < allcheck.length; i++) {
        if (masterCheck.checked == true) {
            allcheck[i].checked = true;
        } else {
            allcheck[i].checked = false;
        }
    }

} 

For some reason, this was the final piece to the puzzle. Thanks for all of the help!

like image 53
Gabriel Avatar answered Mar 16 '26 08:03

Gabriel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!