I have some code that should selected data from an HTML table into a JavaScript array. The code appears to work fine, yet I can not then access the array values by index. The alerts in this jsfiddle show that the contents and length of the array appear as expected. Have I got some kind of Typing error?
https://jsfiddle.net/nicktry/bwdsh5uv/39/
$(function () {
var selectedJobs = [];
//remove job from list
function removeJob(arr, jobValue) {
var p = arr.indexOf("'"+jobValue+"'");
if (p != -1) {
arr.splice(p, 1);
}
}
//add job to list
function addJob(arr, jobValue) {
arr.push("'"+jobValue+"'"); //append
arr = arr.sort(); //sort list
}
$('table tbody tr').click(function (event) {
if (event.target.type !== 'checkbox') {
$(':checkbox', this).trigger('click');
}
});
$("input[type='checkbox']").change(function (e) {
if ($(this).is(":checked")) {
$(this).closest('tr').addClass("selected_row");
addJob(selectedJobs, $(this).parent().next().text());
alert(selectedJobs);
alert(selectedJobs.length);
} else {
$(this).closest('tr').removeClass("selected_row");
removeJob(selectedJobs, $(this).parent().next().text());
alert(selectedJobs);
alert(selectedJobs.length);
}
});
var cluster1Job1 = selectedJobs[0];
var cluster1Job2 = selectedJobs[1];
var cluster1Job3 = selectedJobs[2];
document.getElementById('c1J1').innerHTML = cluster1Job1;
document.getElementById('c1J2').innerHTML = cluster1Job2;
document.getElementById('c1J3').innerHTML = cluster1Job3;
});
Thanks