1

i am creating a string from all the headlines of a table, that making a checkbox to each headline, and its should hide the col if its unchecked and show if its checked. the function put all the checkbox in a div.

i am calling the function when the page is loaded (onload in the body)

this is my function:

function getFirstRow(table) {
    var table = document.getElementById(table);
    var row = table.getElementsByTagName('tr')[0];
    var cells = row.getElementsByTagName('th');
    var str = "";
    for (var i=0; i < cells.length; i++) {
        str += "<input type='checkbox' onclick='hideOrShowCol('TreesTable', "+i+", this);' checked='checked' />" + cells[i].innerHTML + " ";
    }
    document.getElementById("hideAndShow").innerHTML = str;
}

and this is the hide/show function:

function hideOrShowCol(table, col, e) {
    alert(e.checked);
    var ifToShow = "";
    if (e.checked)
        ifToShow = "table-cell";
    else
        ifToShow = "none";
    var getTable = document.getElementById(table);
    var tds = getTable.getElementsByTagName("tr");
    for (var i = 0; i < tds.length; i++) {
        tds[i].childNodes[col].style.display = ifToShow;
    }
}

so the problem is that it is not calling to the function when i am creating the check boxes with the first function, but when i am writing directly in the html its works fine, like this:

<input type="checkbox" onclick="hideOrShowCol('TreesTable', 2, this);" checked="checked" />

some one know what its can be?? i tried everything... thanks.

2 Answers 2

4

"<input type='checkbox' onclick='hideOrShowCol('TreesTable', "+i+", this);' checked='checked' />" is invalid because of the single quotes areound 'TreesTable' nested inside the single quote for the onclick.

Try changing to "<input type='checkbox' onclick='hideOrShowCol(\"TreesTable\", "+i+", this);' checked='checked' />"

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

1 Comment

Even better. Don't use inline JavaScript at all :)
0

try closing over 'i' and passing it to an anonymous self-invoking function:

for (var i=0; i < cells.length; i++) {
    (function(indx) {
        str += "<input type='checkbox' onclick='hideOrShowCol('TreesTable', "+indx+", this);' checked='checked' />" + cells[indx].innerHTML + " ";
    })(i);
}

Comments

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.