Svelte Tutorial for Beginners #16 - forms (part 2)

Net Ninja · Beginner ·🌐 Frontend Engineering ·6y ago

Key Takeaways

Creating forms (part 2) using Svelte

Full Transcript

okay then my friends so now we've seen a couple of different types of inputs and now I'd like to show you two more starting with checkboxes so how do we bind to those well first of all let's create these checkboxes I'll create a label first of all we don't need to fall attribute for now and this is just going to say skills so now we're going to have a series of checkboxes and next to each checkbox is going to be a skill and we tick that checkbox if the person is going to have that skill right so let's create an input type of checkbox first of all and put after this fighting that would be a skill right and then we'll do a br tag so we check this checkbox if they have at the fighting skill I'm gonna duplicate this a couple of times I'm gonna change this one to sneaking and I'm gonna change this one to running like so so let me just save this and preview what it looks like open the model and now we can see we have these skills right here so we check this we're fighting this for sneaking and this running right so now how do we bind to whether the user has checked these tick boxes or not how do we tell that well one way is to use the checked attribute now remember if we have a checked attribute applied to one of these inputs this is either going to be true or false now if I say that this is true then it's gonna be checked right if it's false it's going to be not checked and by default it's false but we can explicitly say false here as well and that will mean it's not checked right so what we could do is bind to this property so much like we're bound to the value in inputs over here we can bind to the checked property or check attribute in a checkbox so we can say bind checked is equal to some kind of value some kind of variable so I'm gonna say let fighting equal false to begin with and then I'm going to pass in here fighting so that is referencing this variable so now this checked property is bound to whatever this is if this is false then this is going to be false this is true this is going to be true and vice versa if we user ticks this and it becomes true checked then this is updated to true make sense right so we can do that for each one of these I'm going to copy and paste these in like so so this will be bound to a variable called sneaking and this one will be bound to a variable called running and we need to create those so let me duplicate those and change this to sneaking and change this one at the bottom to running like so so now we're storing whether a user is checked each one of these in these three variables and if I output these at the end fighting and then sneaking and then running we should see a variation of true false true false true false etc so if I save that and come over here open the modal and I'm just going to enter in a lot of junk here and then if I choose fighting and sneaking then we should see true true false right that person and we see true true false so that's one way of tracking whether a user has checked an input by binding to the checked property now this is okay but it's a little bit messy especially if we have a lot of different checkboxes because then we've got a lot of different properties instead there's another way we can use and that is by using the grouped attribute so let me comment all of this stuff right out and then I'm going to show you a different way of doing this so let me now just grab all of this I'm going to copy it and paste it down here and then uncomment it like so and this time we'll take away binding to the checked property over here we don't want that anymore get rid of that get rid of that and get rid of this and this time we bind to a group property so let me show you how this works bind group and set that equal to some kind of variable so I'm going to call this skills and I'm going to declare this up here let's skills and I'm going to set this equal to an empty array now I will explain how all this works in a second but let me just paste these in down here so bad to the group skills now we need to give each one a value as well so the value of this is going to be fighting we need to give this a value down here so value is equal to this is sneaking and then the value down here is equal to running so what we're doing here is saying all of these different check boxes belong to a specific group and we're binding to that group this variable right here skills which is an array now whatever a user checks the value of that will be added to this group this particular array right here so if we check this and this then the value fighting and the value sneaking will both be added to the skills array so now we're keeping hold of all of the options that our user ticks in one single array which i think is a bit better so if I save this now in fact let's come down here and instead of outputting knows just output skills let me save that and come over here open the model and blahdy blahdy blahdy blah 56 fighting and sneaking at the person now we see we have this array with fighting and sneaking included if we add running and take away sneaking at the person now we have fighting and running and so forth so that is a nicer way of binding to these check boxes by using a group and the same that equal to an array to track these different values okay cool so that is inputs and let me delete all of that junk there because we don't need that anymore the next thing I'd like to show you is select boxes so at the minute a user can enter in their own belt color right here in a text input but instead of that I want to use a select box for the belt color so let me come down below the check boxes and now let's do another label again we don't really need this for and this is going to say belt color then below that we need a select box and this again doesn't really need a name doesn't really need an ID so let's get rid of that and inside a select box we have different options right so let's do our first option and the option value is going to be black and the text that our user sees is also going to be black so let me duplicate this a couple of times let's do four in total this time I want to change this to orange this black on this black so let's change that to orange and then this and this thing over here will change to brown and then this thing this thing will change to white so this is the actual value of the option that we're tracking when a user selects this this is just what the user sees they don't have to be the same but they are in this case because it makes sense okay so now what we want to do is bind to the value of the Select itself so we say bind and then value and set that equal to some kind of variable which we already have the belt color so this thing over here so what's going on here well when a user selects this option from the drop-down then the value of the Select box itself becomes the value of that option so orange so then because we're binding through the value it's going to update this variable up here belt color so whatever a user selects and that's how simple it is so when I output and belt color down here it still should work let me save this and come over here and try this out the name is going to be Mario the age is gonna be 30 will say sneaking and running and the belt is going to be brown at the person and now we see Mario Brown 30s sneaking and running if we change this to black we can see it still tracks it so easy as pie that's how we use check boxes and also select boxes in felt next up we need a way of updating our people data over here in the app components so we'll do that in the next video

Original Description

Hey gang, in this Svelte tutorial we'll carry on with our form & look at 2 more field types - checkboxes and selects - and how to bind to them. 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join ---------------------------------------- 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Course Files: https://github.com/iamshaunjp/svelte-tutorial/tree/lesson-16 🐱‍💻 🐱‍💻 Other Related Free Courses: + HTML & CSS Crash Course - https://www.youtube.com/playlist?list=PL4cUxeGkcC9ivBf_eKCPIAYXWzLlPAm6G + Modern JavaScript - https://www.youtube.com/playlist?list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc 🐱‍💻 🐱‍💻 Svelte Docs https://svelte.dev/tutorial/basics 🐱‍💻 🐱‍💻 The Net Ninja Community Boards: https://community.thenetninja.co.uk/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

Up next
How to Speed Up Your WordPress Website with WP Rocket ⚡Tutorial 2026
Matt Tutorials
Watch →