Skip to content

Commit 8895c1a

Browse files
authored
Add a form wrapper for better UX (#820)
### Description - Adds `<form>` wrapper and `preventDefault` handler - Changes the inline demo and the step-by-step guide to match ### Motivation To have a better UX: pressing Enter in the field now adds an item. Based on the original PR mdn/content#40201 ### Related issues and pull requests - [x] mdn/content#41304 ⚠️ Should be merged together.
1 parent 34186e2 commit 8895c1a

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

javascript/apis/document-manipulation/shopping-list-finished.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020

2121
<h1>My shopping list</h1>
2222

23-
<div>
23+
<form>
2424
<label for="item">Enter a new item:</label>
2525
<input type="text" name="item" id="item">
2626
<button>Add item</button>
27-
</div>
27+
</form>
2828

2929
<ul>
3030

@@ -35,7 +35,9 @@ <h1>My shopping list</h1>
3535
const input = document.querySelector('input');
3636
const button = document.querySelector('button');
3737

38-
button.addEventListener('click', () => {
38+
button.addEventListener('click', (event) => {
39+
event.preventDefault();
40+
3941
const myItem = input.value;
4042
input.value = '';
4143

javascript/apis/document-manipulation/shopping-list.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020

2121
<h1>My shopping list</h1>
2222

23-
<div>
23+
<form>
2424
<label for="item">Enter a new item:</label>
2525
<input type="text" name="item" id="item">
2626
<button>Add item</button>
27-
</div>
27+
</form>
2828

2929
<ul>
3030

0 commit comments

Comments
 (0)