jquery - Unable to get property '0' of undefined or null reference JavaScript Internet Explorer -
javascript code
function update() { var newamt = 0; var newtable = document.getelementbyid("tbl"); ( var = 0; < newtable.rows.length; i++) { innertable = newtable.rows[i].cells[0].childnodes[0]; if ( (innertable.rows[0].cells[5].childnodes[0].value != "") && !(isnotnumber(innertable.rows[0].cells[5].childnodes[0].value))) { newamt = newamt + parsefloat(innertable.rows[0].cells[5].childnodes[0].value); } } </script>
innertable.rows[0] throwing exception unable property '0' of undefined or null reference in internet explorer
but same piece of code working in eclipse web browser
html
<table id="tbl"> <logic:iterate name="editlist" property="list" id="editform"> <tr> <td> <table> <tr> <td></td> <td /></td> <td></td> <td /></td> <td></td> <td><html:text name="amt" onchange="update()" /></td> </tr> <tr> </tr> <tr> </tr> </table> </td> </tr> </logic:iterate> </table>
stuck in 1 past 2 days. fed 1 me
thanks in advance
have try @ simpler unobtrusive code note removed inline event handler
function isnumber(n) { return !isnan(parsefloat(n)) && isfinite(n); } var fields, newamt = 0; // unobtrusive version ignoring table window.onload = function() { fields = document.queryselectorall("[name=amt]"); (var = 0; < fields.length; i++) { fields[i].onkeyup = function() { newamt = 0; (var = 0; < fields.length; i++) { var val = fields[i].value; newamt += val != "" && isnumber(val) ? parsefloat(val) : 0; } console.log(newamt); } } }; // fixing code - not using nodes ie too: function update() { var newamt = 0; var newtable = document.getelementbyid("tbl"); (var = 0; < newtable.rows.length; i++) { var innertable = newtable.rows[i].cells[0].getelementsbytagname('table')[0], input = innertable.rows[0].cells[5].getelementsbytagname('input'), val = input && input.length ? input[0].value : 0; if (val!="" && isnumber(val)) { newamt += parsefloat(val); } } console.log(newamt); }
just start typing numbers <table id="tbl"> <tr> <td> <table> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td><input type="text" name="amt" /></td> </tr> <tr> <td></td> </tr> <tr> <td></td> </tr> </table> </td> </tr> <tr> <td> <table> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td><input type="text" name="amt" /></td> </tr> <tr> <td></td> </tr> <tr> <td></td> </tr> </table> </td> </tr> <tr> <td> <table> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td><input type="text" name="amt" /></td> </tr> <tr> <td></td> </tr> <tr> <td></td> </tr> </table> </td> </tr> <tr> <td> <table> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> <td><input type="text" name="amt" /></td> </tr> <tr> <td></td> </tr> <tr> <td></td> </tr> </table> </td> </tr> </table> <hr> <button type="button" onclick="update()">test fixed code</button>
Comments
Post a Comment