-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathingredient-inputs.js
36 lines (32 loc) · 1.35 KB
/
ingredient-inputs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
document.addEventListener('DOMContentLoaded', function () {
console.log('Yes!');
const addIngredientButton = document.getElementById('addIngredient');
const ingredientInputs = document.getElementById('ingredientInputs');
addIngredientButton.addEventListener('click', function () {
const newIngredientInput = document.createElement('div');
newIngredientInput.className = 'input-group mb-2';
const inputField = document.createElement('input');
inputField.type = 'text';
inputField.className = 'form-control';
inputField.name = 'ingredient[]';
inputField.required = true;
inputField.value = ''; // Set initial value to empty string
inputField.required = true;
const removeButton = document.createElement('button');
removeButton.type = 'button';
removeButton.className = 'btn btn-sm btn-danger remove-ingredient';
removeButton.textContent = 'Remove';
removeButton.addEventListener('click', function () {
ingredientInputs.removeChild(newIngredientInput);
});
newIngredientInput.appendChild(inputField);
newIngredientInput.appendChild(removeButton);
ingredientInputs.appendChild(newIngredientInput);
});
// Add event listener for dynamically added remove buttons
ingredientInputs.addEventListener('click', function (e) {
if (e.target.classList.contains('remove-ingredient')) {
e.target.parentElement.parentElement.remove();
}
});
});