-
Notifications
You must be signed in to change notification settings - Fork 23.1k
Improve form submission example UX #40201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -278,16 +278,18 @@ The finished demo will look something like this: | |||||||||||||||||||||||||||
| To complete the exercise, follow the steps below, and make sure that the list behaves as described above. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| 1. To start with, download a copy of our [shopping-list.html](https://github.com/mdn/learning-area/blob/main/javascript/apis/document-manipulation/shopping-list.html) starting file and make a copy of it somewhere. You'll see that it has some minimal CSS, a div with a label, input, and button, and an empty list and {{htmlelement("script")}} element. You'll be making all your additions inside the script. | ||||||||||||||||||||||||||||
| 2. Create three variables that hold references to the list ({{htmlelement("ul")}}), {{htmlelement("input")}}, and {{htmlelement("button")}} elements. | ||||||||||||||||||||||||||||
| 3. Create a [function](/en-US/docs/Learn_web_development/Core/Scripting/Functions) that will run in response to the button being clicked. | ||||||||||||||||||||||||||||
| 4. Inside the function body, start off by storing the current [value](/en-US/docs/Web/API/HTMLInputElement/value) of the input element in a variable. | ||||||||||||||||||||||||||||
| 5. Next, empty the input element by setting its value to an empty string — `''`. | ||||||||||||||||||||||||||||
| 6. Create three new elements — a list item ({{htmlelement('li')}}), {{htmlelement('span')}}, and {{htmlelement('button')}}, and store them in variables. | ||||||||||||||||||||||||||||
| 7. Append the span and the button as children of the list item. | ||||||||||||||||||||||||||||
| 8. Set the text content of the span to the input element value you saved earlier, and the text content of the button to 'Delete'. | ||||||||||||||||||||||||||||
| 9. Append the list item as a child of the list. | ||||||||||||||||||||||||||||
| 10. Attach an event handler to the delete button so that, when clicked, it will delete the entire list item (`<li>...</li>`). | ||||||||||||||||||||||||||||
| 11. Finally, use the [`focus()`](/en-US/docs/Web/API/HTMLElement/focus) method to focus the input element ready for entering the next shopping list item. | ||||||||||||||||||||||||||||
| 2. Wrap the div that contains the input and button in a form element. | ||||||||||||||||||||||||||||
| 3. Create three variables that hold references to the list ({{htmlelement("ul")}}), {{htmlelement("input")}}, and {{htmlelement("button")}} elements. | ||||||||||||||||||||||||||||
| 4. Create a [function](/en-US/docs/Learn_web_development/Core/Scripting/Functions) that will run in response to the button being clicked. | ||||||||||||||||||||||||||||
| 5. Inside the function body, start off by calling [`preventDefault()`](https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault). When combined with the form element, the user can now use the 'enter' key to submit list items. Prevent default keeps the form from refreshing the page in this situation. | ||||||||||||||||||||||||||||
| 6. Next, continue by storing the current [value](/en-US/docs/Web/API/HTMLInputElement/value) of the input element in a variable. | ||||||||||||||||||||||||||||
| 7. Next, empty the input element by setting its value to an empty string — `''`. | ||||||||||||||||||||||||||||
| 8. Create three new elements — a list item ({{htmlelement('li')}}), {{htmlelement('span')}}, and {{htmlelement('button')}}, and store them in variables. | ||||||||||||||||||||||||||||
| 9. Append the span and the button as children of the list item. | ||||||||||||||||||||||||||||
| 10. Set the text content of the span to the input element value you saved earlier, and the text content of the button to 'Delete'. | ||||||||||||||||||||||||||||
| 11. Append the list item as a child of the list. | ||||||||||||||||||||||||||||
| 12. Attach an event handler to the delete button so that, when clicked, it will delete the entire list item (`<li>...</li>`). | ||||||||||||||||||||||||||||
| 13. Finally, use the [`focus()`](/en-US/docs/Web/API/HTMLElement/focus) method to focus the input element ready for entering the next shopping list item. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||
| > [!NOTE] | ||||||||||||||||||||||||||||
| > If you get really stuck, have a look at our [finished shopping list](https://github.com/mdn/learning-area/blob/main/javascript/apis/document-manipulation/shopping-list-finished.html) ([see it running live also](https://mdn.github.io/learning-area/javascript/apis/document-manipulation/shopping-list-finished.html)). | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.