2

I want the javascript to check only elements from one table if checked the corresponding table's checkbox only. JavaScript i have selects all rows from all the tables generated from a cgi script.

You can see multiple tables with information listed. If you select the checkbox of any below geneID, it checks all even from the other tables contents too. one possible problem i figured out is i have assigned same table id for all the tables, but as this is auto generated from a previous cgi script, i certainly cant find any possible solution to overcome it. Thank you any suggestions or comments are highly accepted.

<script type="text/javascript" language="javascript">
    function SetAllCheckBoxes(ele) {
        var checkboxes = document.getElementsByTagName('input');
        if (ele.checked) {
            for (var i = 0; i < checkboxes.length; i++) {
                if (checkboxes[i].type == 'checkbox') {
                    checkboxes[i].checked = true;
                }
            }
        } else {
            for (var i = 0; i < checkboxes.length; i++) {
                console.log(i)
                if (checkboxes[i].type == 'checkbox') {
                    checkboxes[i].checked = false;
                }
            }
        }
    }
</script>
5
  • Change var checkboxes = document.getElementsByTagName('input'); into selecting the correct table first, then find all the input elements inside that table only. Atm you select all the inputs on the screen, hence you check them all. Commented Aug 28, 2015 at 9:11
  • Having same id multiple times on single page is not a correct way, you should change your cgi script to correct that bug. Commented Aug 28, 2015 at 9:16
  • Yes definitely i looked into cgi-script, but couldn't find any possible way to correct as all tables inside one loop and retrieving information from a mysql database row by row, fixing table id tag i tried assigning a integer after item, like item1, item2 ... but it doesn't iterate and treats it as a string inside that tag. Commented Aug 28, 2015 at 9:23
  • Is there any way your CGI script could add a unique class to each table (such as table name), so you can identify the table you wish to iterate over? Commented Aug 28, 2015 at 10:28
  • Thank You, Tro! The answer by Raef Kandil solved my issue here. And i have assigned class for each table for external css. Commented Aug 28, 2015 at 11:45

2 Answers 2

1

If the code you cannot change, you need to get somehow the table element of the selected checkbox. In order to get that, you will have to alter the javascript function as follows:-

    <script type="text/javascript" language="javascript">
        function SetAllCheckBoxes(ele) {
            var checkboxes = ele.parentNode.parentNode.parentNode.getElementsByTagName('input');
            if (ele.checked) {
                for (var i = 0; i < checkboxes.length; i++) {
                    if (checkboxes[i].type == 'checkbox') {
                        checkboxes[i].checked = true;
                    }
                }
            } else {
                for (var i = 0; i < checkboxes.length; i++) {
                    console.log(i)
                    if (checkboxes[i].type == 'checkbox') {
                        checkboxes[i].checked = false;
                    }
                }
            }
        }
    </script>

First parentNode gets you the td, second parentNode gets you the tr, third parentNode gets you the table.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank You, it generally works for only one table contents on the top. But how can i make it work for other generated tables as well? as you can see for each table i have created one checkbox below geneID.
Anyways, it gave me some idea how to make it work. Thank You
for the other tables, the javascript function is like this: <input type='checkbox' name="checkall" onClick="SetAllCheckBoxes(this.checked);"> instead of: <input type='checkbox' name="checkall" onClick="SetAllCheckBoxes(this);"> and this is why it does not work. Can you change this or is it a part of the generated code that you cannot change?
1

Since you have a separate form for each table you can make use of that to locate only the inputs on that form. Also you don't need the if - just set the checkboxes' checked states to that of ele.

function SetAllCheckBoxes(ele) {
    var checkboxes = ele.form.getElementsByTagName('input');
    for (var i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i].type == 'checkbox') {
            checkboxes[i].checked = ele.checked;
        }
    }
}

1 Comment

Hello James, Thank You very much for your valuable suggestions. I have tried with that but couldn't fix of selecting only rows from one table at a time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.