JavaScript for check and uncheck all checkboxes in the html table

Problem: I have a webpage that contains a html table with the multiple options. Each option has a check box in first column and now i would like to add a link button to select all the options on the page and similarly a link button for unselect all options. How can i do that?

Select and unselect all checkbox in table

JavaScript code:

function marklist(id, state) {
        var parent = document.getElementById(id);
        if (!parent) {
            eval('parent = document.' + id);
        }
 
        if (!parent) {
            return;
        }
 
        var rb = parent.getElementsByTagName('input');
 
        for (var r = 0; r < rb.length; r++) {
            if (rb[r].type == 'checkbox') {
                rb[r].checked = state;
            }
        }
    }