I could use some advice from any javascript guys out there.
I have a javascript function that builds a table from the result of a JSON call. It works as hoped with the exception of
an unexpected space in the web page and an undefined element. I'm assuming that I have somehow screwed up the
loop that goes through the result.
Can anyone suggest a way to troubleshoot this?
} 
Thanks
I have a javascript function that builds a table from the result of a JSON call. It works as hoped with the exception of
an unexpected space in the web page and an undefined element. I'm assuming that I have somehow screwed up the
loop that goes through the result.
Can anyone suggest a way to troubleshoot this?
PHP Code:
function populateTable() {
var xhr1 = new XMLHttpRequest();
xhr1.open('GET', '/JSON?request=getstatus', true);
xhr1.send(null);
xhr1.onreadystatechange = function() {
if (xhr1.readyState == 4) {
var myObj = JSON.parse(xhr1.responseText);
var countKey = Object.keys(myObj).length;
var light_table = document.getElementById('lightTable');
var devString
var i;
var g = 1;
var mycmd = 'off';
//starting table
devString = devString + '<table>';
devString = devString + '<tr><th></th><th></th><th>Device</th><th>Control</th></tr>';
for (i = 0; i < myObj.Devices.length; i++) {
if (myObj.Devices[i].device_type_string == "Z-Wave Switch Binary"||myObj.Devices[i].device_type_string == "Z-Wave Switch Multilevel") {
if (myObj.Devices[i].value == 0){
mycmd = 'on';
console.log('Value:' + myObj.Devices[i].value);
}
myname = myObj.Devices[i].name;
myref = myObj.Devices[i].ref;
myimage = '<img src="' + myObj.Devices[i].status_image +'" style=\'width:32px;height:32px;\'>'
mybutton = '<button class=\'button\' value=\'Toggle\' onClick="deviceCmd(\'' + myref + '\',\' '+ mycmd + '\')">' + mycmd + '</button>' ;
devString = devString + '<tr><td>' +g + '</td><td>' + myimage +'</td><td>' + myname + '</td><td> ' + mybutton + '</td></tr><br>';
g++;
}
}
devString = devString + '</table>';
//finished table
light_table.innerHTML = devString;
}
}
Thanks
Comment