Creating Radio and CheckBoxes
We are getting close to the end of this course but before we do in this lesson we will explore two new input types the radio button and the checkbox.
Video Transcript
00:01if you're like my mom your you wish this recording will be over already so the
00:06good news is this is the last really big thing that we're gonna learn in this
00:09segment of videos course is not over but we're getting close all right so we
00:14learned a lot a lot about a lot of different input types there's a lot more
00:18that we were are not gonna cover because there's just not enough time in
00:21the day to cover every single thing but what I wanted to cover is one basic
00:25controller one basic way of users input which is basically checkboxes and
00:30radio buttons now the difference between a radio button and a checkbox is a check
00:35button that to be a check button basically a check I'm so confusing
00:41myself I check enables you to select multiple options while a radio button
00:45doesn't so let's start with a radio button I know what that is okay it's a
00:49radio button and that's a big difference in a radio button and and and a checkbox
00:54thank you so a radio button is again we're gonna use the input element again
00:59and we're gonna set it a type and we're gonna set the type itself the value of it
01:02to be radio not Rio radio we're gonna give it also a name because everything
01:09needs a name or we won't be able to send the information so we'll put your s for
01:12sex yes please as us in powers would say and the next thing that we want making
01:20myself laugh and the next thing you want to put inside is the value that
01:23we're gonna assign to this element so for example male or female but for us
01:27we're gonna set a value of zero or one zero for man one for female now this
01:33because nothing that the user see this is the information that is going to be
01:36sent out to the server and that's all we had to do with our input element to
01:39create our first input element now there's nothing gonna be we'll have here
01:44a nice little button here if we go and check it in our browser but we're not
01:48gonna get any copy anything to show us what it is and you could actually
01:52select it so it's kind of like the selectable button but it's floating in
01:55the air without anything there so the way that we add copy and by the way
01:58before I continue also notice how I'm we've talked about this previously in
02:03the past how the closing tag is self-enclosed sometimes you'll see that
02:07I or some other people will put the input without putting a self-enclosed
02:11and the reason is because an HTML 5 it's not mandatory anymore because every
02:15input is always a self-enclosed so HTML 5 just to make things more simple
02:20said you don't have to if you don't want to put it in but it's a good
02:23habit to put the self-enclosed tag making sure that you know and you're
02:27aware also that it's a good practice exactly okay so now we're gonna put the
02:32values for example this is gonna be mail and by the way it's not a bad habit as
02:38well to make a connection between the two how do we make a connection between
02:42the two by putting it as a label so I could literally just put your another
02:47label just as much as we had previously and we just set a label
02:51underneath set the value of the label to be male and now the order doesn't
02:57really matter so now when I set it to be male all all that's left is for me to
03:00put your another ID call it sex make sure that our label is for sex and we
03:06have your one male and let's create also a female now so this is gonna be a
03:11radio button the name itself remains the same this is an important part
03:14because the value we're working on the same variable so we're this is a
03:18selection of or this or that and we could have multiple ones but we want to
03:22make sure that we keep it in the same name but the ID itself would change so
03:26this will be ID for sex male while the other one might be ID of sex female
03:37because the tags itself this is for internally for us but the actual
03:40information is being sent to the server is always going to be the same
03:44variable and the value for that variable this time would be one and here we
03:48can make it as female does that make sense perfect so now we have two input
03:53elements if we go back into our browser and click on refresh we'll see that we
03:57have an option between male or female who can select both right if we ever tried
04:02to select both and try not to drop down the lighting around us while we're
04:07doing this if we tried to select it it wouldn't enable to select both at the
04:12same time but if our variable names if our name itself was different for
04:16example if it was one was s and the other was s2 they would act as a
04:20completely independent element so this one would be selectable and that wouldn't
04:24be selectable because they're not the same variable so it's really important
04:28that if you want to cluster the two or more radio boxes together they make
04:32sure that their name is the same okay so that's about radio boxes and I'm
04:37just gonna put here a quick little br and I want to grab this input element
04:41and I'm just gonna copy it in this liberal world that we live in there are
04:46people that are partially male and partially female and it's not fair to
04:50tell them they have to select if they're male or female I personally feel so to
04:56enable them to select multiple options we have to give them an option instead of
04:59using a radio we could actually use a checkbox and to create a checkbox all we
05:04have to do is change the value from radio to checkbox now notice how I
05:13copy and paste it so I copied everything here and I pasted it down here it's
05:17really important that we don't leave the same names because we're ready use the
05:20name for s so we want to use a different names in this this area let's call it
05:24CB for checkbox and recall here CB for checkbox as well we still want to use
05:29the same name because it's the same selection same question that they're
05:32asking if they're male or female and for this one I'm just gonna make sure
05:36that also for my ID because each ID has to be unique so I'm gonna add here
05:40a CB for my sex male and I'm right CB making sure that the connection between
05:45the two remains constant and same goes for our sex female I'll put here a CB
05:50for checkbox before and connect it with that label with writing that again so
05:56literally it you know once you get used to these stuff I did this really
05:59quickly but literally I could have we could have rewrote everything that we
06:03did before just making sure that we have different variable names that's all
06:06we really did now if we go into our browser again and click on refresh this
06:11time around we're gonna have our first option which is our male or female which
06:14is self-inclusive you can only pick one or the other while our next row we could
06:18actually select both female and male so for example if you had a catalog of
06:22some sort of a few catalogs enabling users to select what catalogs do they
06:26want to get okay make sense all right so that covers our radio boxes and
06:31checkboxes we did out all together in one simple quick video which if it was
06:37hard just click on reverse a bit and watch it a couple of times and really the
06:40most important thing is practice open up that browser and type what we said
06:44download our source files and experiment with them you can't learn these
06:47episode without actually hands-on playing with it okay
