Exercise - Add event handlers
Now update your application to add an event handler for a newly created button on your form. The button will toggle the booking.completed
flag to update the display.
Add the function
Start by adding to the Vue application:
In Visual Studio Code, open the index.js file.
On the line after the
TODO: Add methods
comment, add the following code to create thebookCabin
function. This function is the event handler.// TODO: Add methods methods: { bookCabin() { this.booking.completed = true; } }
Notice that
this
is bound to your current application. It provides access to thebooking
object from the data.
Add the button to the form
Now add the button to your form:
In Visual Studio Code, open the index.html file.
On the line after the
TODO: Add button later
comment, add the following HTML to create your button.<!--TODO: Add button later --> <button class="button" type="button" @click="bookCabin">Book now!</button>
Notice the
@click
attribute is bound to thebookCabin
function you created earlier.
Test the page
Now you get to test your updated page!
- Save all files by selecting File > Save all.
- Open the command palette by selecting Ctrl+Shift+P. On a Mac, select Cmd+Shift+P.
- Ensure Live Server is running by typing Live Server and then selecting Live Server: Open with Live Server.
- Open a browser and go to
http://localhost:5500
. The page now displays a button. - Fill out the form and select the button. Notice that the form disappears from the page.
You've now configured an event handler in Vue.