0

The form when submitted pushes the selected values to the object corresponding arrays.

But when I enter text into the comment input field after I have submitted the form the text which was just entered stays on screen.

Im not sure if it is possible to do as I prevent the page from refreshing.

Note: The end goal is to save this information in cookies but that is a task for a different day.

<HTML>
        <form id="mainForm" action="#">
            <select id="menuMain" name="foodOrder">
                <option>Mains</option>
            </select>
            <select id="waiters" name="waiterSelection">
                <!--<option>Waiters</option>-->
            </select>
            <input type="text" id="comment">
            <input type="submit" value="Submit">
        </form>

<JS>
const formEl = document.getElementById('mainForm');
const comment = document.getElementById('comment');
formEl.onsubmit = function (e) {

    //Selecting the comment input on the form to pass to the 
    //comment waiters array.
    const comment = document.getElementById('comment').value;


    //Selecting the choosen index from the user food and which waiter orderd //it which waiter.
    //Selects the choosen food to pass to the addFood method in the waiter //class.
    const foodItemIndex = foodMain.options[foodMain.selectedIndex].value;
    const foodItem = mainFood[foodItemIndex];


    //Selecting the waiter to push valid informaiton to.
    const waiterName = waitersEl.options[waitersEl.selectedIndex].value;
    const waiter = waiters.find(({name}) => name === waiterName);




    //Logic to check when submited if both feilds are true proceed.
    //The statements check to see which feild values have been entered
    //Then it call's the corresponding method from the waiter class and
    //pushes the value to the choosen array
    if (waiter && foodItem && comment) {
        waiter.addFood(foodItem)
        waiter.addComment(comment);
        console.log(waiters);
    }
    else if (waiter && comment) {
        waiter.addComment(comment);
        console.log(waiters);

    }
    else if (waiter && foodItem){    
        waiter.addFood(foodItem)
        console.log(waiters);
    }




    return false; // prevents redirect/refresh



};

The expected result is just to have a clear input field after submission

Liam
  • 568
  • 2
  • 15

0 Answers0