Description: In this task you add a new phone model to the phones array, and also move a phone models to the sold phones box You'll learn the followings from this task:
- Adding an item to the start of an array using reverse method.
- Using
preventDefaultto prevent the default action for an event.
In the HTML, add a form element before the .list-holder element and set the class attribute to flex. Inside the form element:
- Create an input element with a placeholder
Add a phone modeland set the class attribute toflex-1 border border-gray-500 p-2 pl-5 - Create a button element after the input, set the text to
INSERTand the class attribute topx-4 appearance-none hover:bg-gray-200 border border-gray-900.
- Create an
activeInsertForm()function. - Get the
<form>and<input>elements from the DOM and assign them to their respective variables. - Add a
submitevent to the<form>element (this event is triggered before the form submits). Inside the eventclosure, get the value of the<input>element from it's variable. Copy and paste the code below next.
const phones = PhoneHolder.get();The
phonesabove variable is simple anArray.
- You are expected to
pushthe value of the<input>element to thephonesarray. Copy and paste the code below next.
PhoneHolder.set(phones);- Clear the
<input>elements value by settings it'svalueproperty to an empty string.
formInput.value = ''- Still in the
closureinvoke therefreshList()function to render the list to the DOM.
Head to your browser to see what the code does for now.
Am sure you must have noticed that whenever the form is submitted it reloads the page(Form elements behaviour by default). We'll have to stop that from happening.
- Back to the
submiteventclosurewhich looks something like this:
formElement.addEventListener('submit', function() {
...- You have to access the
eventparameter. This parameter is passed to theclosurewhen an event is fired. So we get:
formElement.addEventListener('submit', function(event) {
event.preventDefault();
...At this point, we should be able to insert a new phone model even if the input field is empty. The input will be added to the end of the list.
- Ask google how to insert an item to the start of an array in javascript.
- Create an activateAddButton function before the
activeInsertFormfunction. This function has two parameters,listElandmodelwhich areNodeandStringdatatypes respectively. - In the
activateAddButton()function, query select theadd buttonwith[role=add-to-sold]from thelistEl. - In the click event
closureinvoke themovePhoneToSold()with an argument ofmodel. - Head to the
createList()function to uncomment theactivateAddButtonstatement.
- Create the
movePhoneToSold()function above theactivateAddButton()function. This function should have a parameterphone_namewhich will be ofStringdatatype. - Create
filterPhonesvariable. Set the variable to an empty array. - Run a
forloop through thePhoneHolder.get() - Inside the loop block, write a code that pushes the item that isn't equal to the
phone_nameparameter. - Invoke
PhoneHolder.set()function with thefilterPhonesvariable as the argument. - Create a
soldListvariable and assign it toPhoneHolder.getSold()function, which returns anArray. - Push the
phone_namevariable to thesoldListvariable. - Invoke the
PhoneHolder.setSold()function withsoldListas the argument. - Invoke the
refreshList()function.
Some changes where made to the
refreshList()and theaddToList()function. Do well to understand them.
The next task should have some AJAX. 🤔