Just to provide some background, the Bootstrap CDN is connected in my code, but unfortunately I can't paste it here at the moment.
As I run this script, the initial submission successfully generates a table row with the id='masterTable'
. However, upon subsequent submissions, the content just gets appended to the same row instead of creating a new one.
I'm trying to figure out how to make a new table row every time the submit button is clicked, but I seem to be stuck. Any suggestions?
<!DOCTYPE html>
<html>
<link>
</link>
<head>
</head>
<body>
<div class="d-flex">
<div>
<select id="ddlViewBy">
<option value="0310">XXXX-10</option>
<option value="4610" selected="selected">XXXX-10</option>
<option value="4610">XXXX-10</option>
</select>
</div>
</div>
<form>
<div class="form-group">
<label>Hand Trucks</label>
<input type="text" class="form-control" id="handtrucks" placeholder="Enter
number of Hand Trucks.">
</div>
<div>
<label>Furniture Pads</label>
<input type="text" class="form-control" id="furniture" placeholder="Enter
number of Furniture Pads.">
</div>
<button type="submit" onClick='addLocation(); return false;' id='button'
class="btn btn-primary">Submit</button>
</form>
<tbody id='masterTable'>
</tbody>
</table>
</body>
<script>
const button = document.getElementById('button');
const dropdown = document.getElementById('dropdown').value;
const htruck = document.getElementById('handtrucks').value;
const fpad = document.getElementById('furniture').value;
function addLocation() {
let masterList = document.getElementById('masterTable');
var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].value;
var handTruck = document.getElementById('handtrucks').value;
var furnPads = document.getElementById('furniture').value;
var row = document.createElement("tr");
var locationEntry = document.createElement('td');
var htEntry = document.createElement('td');
var fpEntry = document.createElement('td');
row.appendChild(locationEntry);
locationEntry.appendChild(document.createTextNode(strUser));
row.appendChild(htEntry);
htEntry.appendChild(document.createTextNode(handTruck));
row.appendChild(fpEntry);
fpEntry.appendChild(document.createTextNode(furnPads));
masterList.appendChild(locationEntry);
masterList.appendChild(htEntry);
masterList.appendChild(fpEntry);
}
</script>
</html>