I am struggling to get this working and would like to know if i'm headed in the right direction with what i have.
Whats supposed to be happening is two input fields that get added to a on a click event. This part is done and its working fine.
Whats supposed to happen after the input fields get added to the table is you can then delete a tr with a button that gets added when the tr is created. Which is not working. I also cant figure out why the page refreshes when you click the delete button to delete a tr, preventDefault() is being used.
let data = [];
let speciesInput = document.querySelector('#speciesInput')
let breedInput = document.querySelector('#breedInput')
let messageBox = document.getElementById("display");
const addProductBtn = document.querySelector('#addValue')
let deleteProductBtn = document.getElementsByClassName('deleteProduct')
const deleteBtnArray = Array.from(deleteProductBtn)
addProductBtn.addEventListener('click', function (e) {
e.preventDefault();
let species, breed;
species = speciesInput.value;
breed = breedInput.value;
data.push({
species: species,
breed: breed,
});
clearAndShow();
})
function clearAndShow() {
// Clear our fields
speciesInput.value = "";
breedInput.value = "";
messageBox.innerHTML = computeHTML();
}
function computeHTML() {
let html = "<table>";
data.forEach(function (item) {
html += "<tr>";
html += "<td>" + item.species + ' ' + "</td>"
html += "<td>" + item.breed + "</td>"
html += "<td>" + "<button class='deleteProduct' onclick='deleteProduct()'> − </button>" + "</td>"
html += "</tr>"
console.log(deleteBtnArray)
});
html += "</table>"
return html;
}
function deleteProduct() {
console.log(deleteBtnArray)
deleteBtnArray.forEach((btn) => {
btn.addEventListener('click', function (e) {
e.preventDefault();
console.log('Delete item')
deleteItem() // Not made yet
})
})
}
deleteProduct()
document.querySelector('#productForm').addEventListener('submit', function (e) {
e.preventDefault();
})
<!-- Add Product -->
<div class="">
<div class="d-flex flex-column justify-content-center align-content-center">
<div class="intro-wrapper">
<h1 class="welcome-heading">Welcome <span class="brand-heading"></span><span class="brand-heading brand-heading-grey"></span></h1>
<span class="intro-description">Set up your profile to begin interacting with the community.</span>
</div>
<span class="secondary-txt mt-5 mb-2">Add Your Product For Display</span>
<form id="productForm" class="form d-flex flex-column align-items-center mt-2" action="">
<div class="product-field-outter">
<div class="form-field product-field-inner d-flex flex-column align-items-center">
<div style="width: 100%!important;" class="form-field my-auto">
<label for="species"></label>
<input id="speciesInput" class="sign-up-inputs addProductInputs" type="text" placeholder="Species">
</div>
<div style="width: 100%!important;" class="form-field my-auto">
<label for="breed"></label>
<input id="breedInput" class="sign-up-inputs addProductInputs" type="text" placeholder="Breed">
</div>
<small></small>
<button id="addValue" class="add-product-btn btn-active mt-4 ms-2">+</button>
</div>
</div>
<div id="display" class="output d-flex flex-column justify-content-center mt-5"></div>
<!-- <ul id="list" class="productList justify-content-between"></ul> -->
<button class="primary-btn continue-btn btn-active mt-4">Continue</button>
</form>
<button class="back-btn btn-active me-auto mt-4">Back</button>
</div>
</div>
</div>
<!-- Add Product -->
<div class="">
<div class="d-flex flex-column justify-content-center align-content-center">
<div class="intro-wrapper">
<h1 class="welcome-heading">Welcome<span class="brand-heading"></span><span class="brand-heading brand-heading-grey"></span></h1>
<span class="intro-description">Set up your profile to begin interacting with the community.</span>
</div>
<span class="secondary-txt mt-5 mb-2">Add Your Product For Display</span>
<form class="form d-flex flex-column align-items-center mt-2" action="">
<div class="product-field-outter">
<div class="form-field product-field-inner d-flex flex-column align-items-center">
<div style="width: 100%!important;" class="form-field my-auto">
<label for="species"></label>
<input id="speciesInput" class="sign-up-inputs addProductInputs" type="text" placeholder="Species">
</div>
<div style="width: 100%!important;" class="form-field my-auto">
<label for="breed"></label>
<input id="breedInput" class="sign-up-inputs addProductInputs" type="text" placeholder="Breed">
</div>
<small></small>
<button id="addValue" class="add-product-btn btn-active mt-4 ms-2">+</button>
</div>
</div>
<div id="display" class="output d-flex flex-column justify-content-center mt-5"></div>
<!-- <ul id="list" class="productList justify-content-between"></ul> -->
<button class="primary-btn continue-btn btn-active mt-4">Continue</button>
</form>
<button class="back-btn btn-active me-auto mt-4">Back</button>
</div>
</div>
</div>
Javascript
let data = [];
let speciesInput = document.querySelector('#speciesInput')
let breedInput = document.querySelector('#breedInput')
let messageBox = document.getElementById("display");
const addProductBtn = document.querySelector('#addValue')
let deleteProductBtn = document.getElementsByClassName('deleteProduct')
const deleteBtnArray = Array.from(deleteProductBtn)
addProductBtn.addEventListener('click', function (e) {
e.preventDefault();
let species, breed;
species = speciesInput.value;
breed = breedInput.value;
data.push({
species: species,
breed: breed,
});
clearAndShow();
})
function clearAndShow() {
// Clear our fields
speciesInput.value = "";
breedInput.value = "";
messageBox.innerHTML = computeHTML();
}
function computeHTML() {
let html = "<table>";
data.forEach(function (item) {
html += "<tr>";
html += "<td>" + item.species + ' ' + "</td>"
html += "<td>" + item.breed + "</td>"
html += "<td>" + "<button class='deleteProduct' onclick='deleteProduct()'> − </button>" + "</td>"
html += "</tr>"
console.log(deleteBtnArray)
});
html += "</table>"
return html;
}
function deleteProduct() {
console.log(deleteBtnArray)
deleteBtnArray.forEach((btn) => {
btn.addEventListener('click', function (e) {
e.preventDefault();
console.log('Delete item')
deleteItem() // Not made yet
})
})
}
deleteProduct()