You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 15-Communicating-with-Events/README.md
+30Lines changed: 30 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -109,3 +109,33 @@ We need to add in a parameter `changeColor(event)`, usually you'll see **e** or
109
109
-**keypress** for something to be considered a keypress, there needs to be a character showing up in this input or a change like space
110
110
111
111
## 8. Coin Game Demo
112
+
113
+
## 9. Form Events & PreventDefault
114
+
115
+
A form is made to take data that we get from an input and submit it or send it somewhere.
116
+
117
+
Traditionally, we would submit our data to some URL like /signup => this would be an endpoint on our own server. The data would be sent to /signup and then on our server side we would do something, create a new user in the database and then respond with a new page that would load and so you would end up going to a completely new page
118
+
119
+
So what we'll do with JavaScript is take the form when it's submitted, will say stop, don't actually send the request, don't take us anywhere else, i just want to capture that event, i want to trigger some JavaScript code, when the user submits the form, then we can extract the information we want and do something with it.
120
+
121
+
Whether it's simply to display it back to the user, whatever we're doing here or maybe we'll send the data to a database somewhere to a server, but we won't actually refresh the page.
122
+
123
+
We'll do it using JavaScript on the same page we're currently on and we could maintain a single page application.
**e.preventDefault()** prevent the default behavior and the default behavior in this case with a form when it's submitted, is to continue sending on your data, sending a request to whatever URL we specified in our case /no with a GET request
136
+
137
+
Our form has not actually submitted, the submit event has run but we haven't actually submitted data anywhere and this leaves us free to now extract data adn send it to an API using AJAX, using client side request
138
+
139
+
The point is we have the flexibility to do something with the data and we can still capture the submit event
140
+
141
+
By adding just one submit event listener, there's just one event we're waiting for to extract our data.
0 commit comments