Update: I seem to have fixed it by simply adding
document.getElementById('fullName').focus();
NameSalaries.sort();
}
in the main function. I can't believe it actually worked. But I assume it's because I 'zipped' the main arrays. Thanks to everyone for your help!
Okay, I'm trying a new method. This way, the employees are added dynamically with a new table cell each time. The issue I'm having remains, however. I need the results to sort themselves as they are submitted. Both the name, and the salaries.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Table Entry </title>
</head>
<body>
<input type="text" id="fullName" value="" placeholder="Firstname"> :Enter employee name <br>
<input type="text" id="salary" value="" placeholder="Salary"> :Enter employee salary <br>
<input type="button" value="click me add to array" onclick="addToArray()"> <p> </p>
<table border="1" id="source" cellspacing="3" cellpadding="3">
<thead>
<tr> <th>Employee Name</th> <th>Employee Salary</th> </tr>
</thead>
<tbody id="sourceBody">
<tr><td> </td><td> </td></tr>
</tbody>
</table>
<script type="text/javascript">
var NameSalaries= [];
function addToArray() {
var fn = document.getElementById('fullName').value;
var sly = document.getElementById('salary').value;
var both = [fn,sly];
NameSalaries.push(both);
createBody(NameSalaries);
document.getElementById('fullName').focus();
}
function createBody(NS) {
var tarr = [];
for (var i=0; i<NS.length; i++) {
tarr.push('<tr><td>'+NS[i][0]+'</td><td>'+NS[i][1]+'</td></tr>');
} document.getElementById('sourceBody').innerHTML = tarr.join('');
}
</script>
</body>
</html>