javascript - Table Cell loop produces undefined index erroneously -
$('#process').click(function() { var char_matrix = []; $('#char_input tr').each(function(e) { $(this).children('td').each(function() { var = $(this).css('background-color').replace(/\d+/g, ''); if (a === '2551650') { char_matrix[e] += "0"; } else { char_matrix[e] += "1"; }//end if });//end each alert(char_matrix[e]); });//end each });//end func
this code loops through each table row, , each row cell create array of 1's , 0's. each index of array different row, , each index composed of series 1's , 0's indicating background color status of each row. expected output like:
10101010 10101010 10101010 10101010 10101010 10101010 10101010 10101010
actual output is:
undefined10101010 undefined10101010 undefined10101010 undefined10101010 undefined10101010 undefined10101010 undefined10101010 undefined10101010
i have no idea causing each row have undefined @ beginning, , quite annoying. undefined index added array, in addition correct 8 binary values. have tried defining char_matrix global , local variable no avail.
i realize can use straight javascript, not option me on project.
after line
$('#char_input tr').each(function(e) {
add line
char_matrix[e] = ""
otherwise char_matrix[e]
undefined. consistent results since
undefined + "1" === "undefined1"
Comments
Post a Comment