I'm creating a page with a form where user would enter name (iname) and price (iprice) for an item in a form that dynamically so the user could enter as many as the user would wish. So the flow goes as the following:
the page load with a form that shows one input to enter item's name (iname)
up on clicking outside the name input, it triggers onblur event that run another funciton to add an input to enter the price of the item
3- in that function I want to run if/else (which I'll describe shortly) where it will run onclick to submit the form results if the save button clicked or rerun the function to create another input for the user to keep entering items name and price.
4- that part with the if/else is what I need help with and just in plain js (no frameworks or jquery please)
my code:
<script>
import { onMount } from 'svelte';
function getinfo(){
let x = document.querySelectorAll('input')
for (let i = 0; i < x.length; i++) {
let values = x[i].value;
// persist the added values to db, localstorage...etc
// for now, just log it
console.log("input-value: ", values)
}
}
onMount( () => {
//create a name input, append it, add onblur fn that create price input
var iname = document.createElement('input')
iname.classList.add("name", "border", "space")
var parent = document.getElementById("parent");
parent.appendChild(iname);
// Once you click outside or hit tab, create price input.
iname.onblur = function () {
addprice()
}
}
)
function additem(){
//Create an input, add classes and set its focus
var parent = document.getElementById("parent");
var iname = document.createElement('input')
iname.classList.add("name", "border", "space")
parent.appendChild(iname);
iname.focus()
// Once you click outside, add another price input(run addprice)
iname.onblur = function () {
addprice()
};
}
function addprice() {
//Create price input, add classes and set its focus
var parent = document.getElementById("parent");
var iprice = document.createElement('input');
parent.appendChild(iprice);
iprice.classList.add("price", "border")
iprice.focus()
let btn = document.getElementById("button")
//*****************************************//
// Need Your Help With This Part //
// if btn is clicked, run getinfo()
if (btn.clicked === true) {
btn.onclick = function () {
getinfo();
}
} else {
// if btn not clicked, run onblur to add another item input
iprice.onblur = function () {
additem()
}
} // end of else block
} // end of addprice()
</script>
<h1> Enter Menu Items </h1>
<form id="mainform" name="formcollect">
<div class="parent" id="parent">
</div>
<button id="button" class="space" type="button" >SAVE</button>
</form>
I'm using svelte/sapper for this page but it's plain old js code that runs it. is btn.clicked=== true is the wrong condition. Because if I run the code with btn.onclick() part alone, it does what the code suppose to do and run the getinfo() with the entered value logged. If I run iprice.onblur() it adds a new input and I keep doing that loop (create iname input and iprice input) but if I add if/else blocks as I included here in the code above, it only runs the onblur function and I keep creating inputs and button doesn't fire the getinfo().
Please watch this video I created so the issue is clear because it is hard to describe. Watch Youtube video describing the issue Notice that when I click save (I'm done creating input and entering data) and want to save the form, the save button keeps creating inputs. How do I submit once I click save?