Italo Ayres
Italo Ayres

Reputation: 2663

JavaScript returning Undefined on getElementsByName()

I have this check boxes printed by a php function, and i want the first one to be a "check all" comand. So, this is my code simplified:

<input id='checkall' type='checkbox' onclick="checkAll();">

<?php
while (loop)
    echo "<input name='invited' type='checkbox' value='".$var."'/>";
?>

<script>
    function checkAll() {
        var aChk = aChk.getElementsByName("invited");
        for (var i=0;i<aChk.lenght;i++) {
        aChk[i].checked = document.getElementById("checkall").checked;
    }
</script> 

I made it short, but all code is correctly organized inside the page, the script is inside , but i keep getting an empty return from aChk.getElementsByName("invited");

I want it to when someone click on the 'checkall', change all check boxes to the actual 'checkall' state.

If i didnt made it clear, or you need more information, just ask.

Upvotes: 0

Views: 606

Answers (1)

Shen
Shen

Reputation: 174

There seems to be a braces missing at the end. Also, 'lenght' should be 'length'. I'm not sure if this helps but let me know.

function checkAll() {
    var aChk = aChk.getElementsByName("invited");
    for (var i=0;i<aChk.length;i++) {
        aChk[i].checked = document.getElementById("checkall").checked;
    }
}

Upvotes: 1

Related Questions