Skip to content

Commit b8553fb

Browse files
committed
First draft of forms exercises
1 parent b14a693 commit b8553fb

24 files changed

+183
-191
lines changed

css/week3-forms.css

+10
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,23 @@ textarea:focus {
8787
box-shadow: inset 2px 0 0 #4491db;
8888
}
8989

90+
input[type="checkbox"],
91+
input[type="radio"] {
92+
vertical-align: middle;
93+
}
94+
9095
fieldset {
9196
margin-left: 0;
9297
margin-right: 0;
98+
padding: 1em;
9399
border: 1px solid rgba(0,0,0,0.5);
94100
border-radius: 3px;
95101
}
96102

103+
fieldset + fieldset {
104+
margin-top: 2rem;
105+
}
106+
97107
legend {
98108
padding: 0 1em;
99109
}
File renamed without changes.

images/19/solution.png

7.89 KB
Loading

images/20/solution.png

3.26 KB
Loading

images/21/solution-1.png

13.2 KB
Loading

images/21/solution-2.png

17.1 KB
Loading

images/22/solution-1.png

49.8 KB
Loading

images/22/solution-2.png

50.1 KB
Loading

images/23/solution.png

21.6 KB
Loading

images/24/solution-delivery.png

5.06 KB
Loading

images/24/solution-required.gif

24.1 KB
Loading

week-3/19-checkout/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ <h1 class="shopping-basket__title">Your Shopping Basket</h2>
2424
<tr>
2525
<td>
2626
<div class="shopping-basket__product">
27-
<img src="/images/19-shirts.jpg" alt="CodeYourFuture t-shirts in black"/>
27+
<img src="/images/19/shirts.jpg" alt="CodeYourFuture t-shirts in black"/>
2828
<p>CodeYourFuture t-shirt, black</p>
2929
</div>
3030
</td>

week-3/19-checkout/readme.md

+10-52
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,16 @@
11
# Checkout Form
22

3-
Notes:
4-
- combines 1/2 from carme's exercises.
5-
- write a bit of JS to check for correct labels
3+
This customer is ready to buy a CodeYourFuture t-shirt. We need ask them who they are and how we can contact them so that we can send them the t-shirt.
64

7-
## Exercise 1
5+
In this exercise you will add forms to collect checkout information. Follow the steps below to complete this exercise.
86

9-
We have seen how to create an `<input type="text">` field. Use what you have learned to start creating the **Check Out** for the CodeYourFuture shop.
7+
1. Open `index.html` and find the line that says `<!-- Add the form fields here -->`.
8+
2. Add an `<input>` field for the customer's name below this line. It should be a `text` field with a `name` attribute set to `customer-name`.
9+
3. Add an `<input>` field for the customer's email address. It should be an `email` field with a `name` attribute set to `customer-email`.
10+
4. Add an `<input>` field for the customer's phone number. It should be a `tel` field with a `name` attribute set to `customer-phone`.
11+
5. Did you remember to add a `<label>` for each one of your fields? If not, go back now and give each field a label, taking care to link each field to its label by using the `for` and `id` attributes.
12+
6. (Optional) Make the form easier to read by using `<p>` or `<div>` tags to add space between each field.
1013

11-
Please notice that, because the data entered on this form is going to be used and stored by the server, we will need to use set the `method="POST"` instead of `GET`.
14+
When you're done, it should look like this.
1215

13-
1. Open `index.html` and add a form tag where it says `<!-- Your new form goes here -->`.
14-
15-
2. Add an input field to the form for the customer's first name. What type of input field will we need?
16-
17-
3. Add a second field for the customer's surname.
18-
19-
4. Add another field for the customer's email.
20-
21-
5. Add an input field for the customer's contact telephone number.
22-
23-
6. Finally, add the Submit button at the end of the form.
24-
25-
26-
## Exercise 2
27-
28-
With the HTML tags we have seen so far, we can build a fully functioning form. We now need to make it user-friendly.
29-
30-
For this, we will add a **label** for each field:
31-
32-
`<label for="target-id">Some text here</label>`
33-
34-
Labels are used to tell the user what data they are expected to enter in that field. The value of the `for` attribute is the `id` of the field to which the label makes reference. For example:
35-
36-
```
37-
<form method="POST" action="/checkout">
38-
<label for="first-name">First Name:</label>
39-
<input type="text" name="first-name" id="first-name">
40-
<input type="submit" value="Submit">
41-
</form>
42-
```
43-
44-
The `label` in the example above is linked to the `input` field with id `first-name`.
45-
46-
To the user it will look like this:
47-
48-
![Form example](../../img/form-example-1.png)
49-
50-
### Have a go!
51-
52-
The form from exercise 1 works well but it is not user friendly. How does anyone know what they are supposed to enter in each field?
53-
54-
1. Open exercise 2's `index.html` and add the form you created in [exercise 1](../1-input-fields/index.html) where it says `<!-- Your exercise 1 form goes here -->`.
55-
56-
2. Add a label to all the input fields that need one.
57-
58-
3. (Optional) Make the form easier to read by displaying each label+input pair in its own line. There are several ways of achieving this, how would you do it? Have a think before Google-ing it!
16+
![Screenshot of the checkout form solution](/images/19/solution.png)

week-3/20-comments/readme.md

+8-25
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,13 @@
11
# Blog Comments
22

3-
The `<textarea>` is used for bigger chunks of text that normally take up more than only line.
3+
The `<textarea>` is used for text that takes up more than one line. In this exercise, you'll add a form with a `<textarea>` for someone to leave their comment. Follow the steps below to complete this exercise.
44

5-
An example of this would be the Comments box:
5+
1. Find the line that says `<!-- Your comment form goes here -->` in this exercise's `index.html` file.
6+
2. Add a `<form>` with a `GET` method. _You do not need to add an `action` attribute to this form._
7+
3. Add a `<textarea>` field that is named `comment`.
8+
4. Don't forget to add a `<label>` so that users know what the field is for.
9+
5. Add a `<button>` that says "Send Comment".
610

7-
```
8-
<form action="/comments" method="POST">
9-
<textarea name="comment" id="comment" cols="45" rows="8"></textarea>
10-
<input type="submit" name="submit" value="Post Comment">
11-
<input type="hidden" name="comment_article_ID" value="2741">
12-
</form>
13-
```
11+
When you're done, it should look like this.
1412

15-
> Have a look at the example above. What do you think the **cols** and **rows** attributes are for?
16-
And what do you think the **hidden** input filed does?
17-
18-
## Have a go!
19-
20-
Shock, horror! The first article has been published in the CodeYourFuture blog, but we have no comments form!
21-
22-
We'll need to have a break from the CodeYourFuture checkout to sort it out.
23-
24-
1. Open exercise 3's `index.html` and add a `form` tag where it says `<!-- Your comment form goes here -->`.
25-
26-
2. Add a `textarea`. Play around with the textarea columns and rows and see how it changes!
27-
28-
3. Add a `label` so that users know what it is for.
29-
30-
3. Add a Submit button.
13+
![Screenshot of the blog comment form solution](/images/20/solution.png)

week-3/21-dietary/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<main>
1414
<h1>CodeYourFuture Summit</h1>
1515

16-
<p>Please let us know if you have any dietary requirements. Thanks!</p>
16+
<p>Let us know if you have any dietary requirements so that we can provide you with the best, yummy food.</p>
1717

1818
<form>
1919
<!-- Your multiple choice goes here -->

week-3/21-dietary/readme.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
11
# Dietary Requirements
22

3-
Notes: use <fieldset> for checkboxes
3+
When using the `checkbox` or `radio` input types, you often need to group the options together. In this exercise, you will use a `<fieldset>` and `<legend>` to describe a group of options. Follow the steps below to complete this exercise.
4+
5+
1. Add a `checkbox` input with a `<label>` for each of the following dietary requirements: Halal, Kosher, Vegetarian, Vegan, Gluten Free.
6+
2. Add a `<fieldset>` with a `<legend>` to describe all of the fields. The legend should say "Please select all of your dietary requirements".
7+
8+
When you are done, it should look like the image below.
9+
10+
![Screenshot of the dietary needs after the first steps](/images/21/solution-1.png)
11+
12+
If you have used the `for` and `id` attributes properly, you should be able to click on the name of each dietary requirement to check that box.
13+
14+
_Hint: If you click on one dietary requirement and it checks a different box, you may have used an `id` more than once. Each `id` on the page has to be unique._
15+
16+
You may notice, however, that there is a small space between the checkbox and the label where you can click and the checkbox will not be changed. This can be frustrating for users. In this case, you can nest the checkbox _inside_ of your label.
17+
18+
```
19+
<label>
20+
<input type="checkbox" name="dietary">
21+
Halal
22+
</label>
23+
```
24+
25+
When `<input>` field is nested inside of a `<label>`, you don't have to use the `id` and `for` attributes. Follow the steps below.
26+
27+
1. Change your code so that each `<input>` is nested inside of a `<label>`.
28+
29+
When you're done, it should look exactly the same except that when you click on the gap between the checkbox and the text it still checks the box.
30+
31+
## Extra Credit
32+
33+
Can you make each item appear on it's own line like the image below?
34+
35+
![Screenshot of the dietary needs with each item on its own line](/images/21/solution-2.png)

week-3/22-shirt-size/index.html

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<title>22 - T-Shirt Size - HTML, CSS and Git Exercises</title>
77
<link rel="stylesheet" href="/css/normalize.css">
88
<link rel="stylesheet" href="/css/week3-forms.css">
9+
<link rel="stylesheet" href="styles.css">
910
</head>
1011

1112
<body>
@@ -29,7 +30,15 @@ <h1 class="shopping-basket__title">Your Shopping Basket</h2>
2930
</div>
3031
</td>
3132
<td>
32-
Medium
33+
<!-- Medium -->
34+
<form>
35+
<label class="screenreader">Choose a size</label>
36+
<select>
37+
<option>Small</option>
38+
<option selected>Medium</option>
39+
<option>Large</option>
40+
</select>
41+
</form>
3342
</td>
3443
<td>
3544
1

week-3/22-shirt-size/readme.md

+26-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
# T-Shirt Size
22

3-
Notes: change the instructions to reflect shirt sizes and replacing the size in the checkout
3+
The `<select>` field is used when you want the user to choose an option but you can not show all of the options at once. In this excercise, you will use the `<select>` field to let the user change the size of the t-shirt they want to order. Follow the steps below to complete this exercise.
44

5-
The `<select>` field is used for a dropdown list. Each of the items on the list is defined by an `<option>` field.
5+
1. Find the table cell where the t-shirt size is mentioned. _Hint: It is "Medium"._
6+
2. Replace the t-shirt size with a form that includes one `<select>` field with the following options: Small, Medium, Large.
7+
3. Use the `selected` attribute to make the Medium option selected by default.
68

7-
An example of this could be a choice of size for an online shop checkout:
9+
When you are done, it should look like the image below.
10+
11+
![Screenshot of the size selection](/images/22/solution-1.png)
12+
13+
It looks like you don't need the label "Choose a size". The table already tells us this column is for "Size". However, users who can not see use assistive technology to browse the web. This technology is often called a "screen reader". It reads what is on the screen for them. These tools do not work for forms unless _every_ field has a label.
14+
15+
In this case, we need to keep the label in the HTML code, but we want to hide it to sighted users. The following CSS code can do that.
816

917
```
10-
<form action="/product" method="POST">
11-
<select id="native_dropdown_selected_size" name="dropdown_selected_size">
12-
<option value="-1">Please select your shoe size</option>
13-
<option value="3">3</option>
14-
<option value="4">4</option>
15-
<option value="5" selected>5</option>
16-
<option value="6">6</option>
17-
</select>
18-
</form>
18+
.screenreader {
19+
clip: rect(1px, 1px, 1px, 1px);
20+
position: absolute !important;
21+
left: -2000px;
22+
}
1923
```
2024

21-
> What do you think the **selected** attribute does?
25+
_Hint: You may have heard of `display: none`. This will also hide an element. However, using this property will hide it for screen readers too! We must use the CSS code above to hide something visually without hiding it from screen readers._
26+
27+
Follow the steps below.
28+
29+
1. Add the CSS code above to the `styles.css` file.
30+
2. Use the class name to hide the label without removing it from the HTML code.
31+
32+
When you are done, it should look like the image below -- _but the `<label>` should still be in the HTML code._
33+
34+
![Screenshot of the hidden label](/images/22/solution-2.png)

week-3/22-shirt-size/styles.css

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* Add your CSS below */
2+
.screenreader {
3+
clip: rect(1px, 1px, 1px, 1px);
4+
position: absolute !important;
5+
left: -2000px;
6+
}

week-3/23-checkout-groups/readme.md

+9-36
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,16 @@
11
# Checkout Groups - Fieldset
22

3-
Note: fieldset has been used in checkboxes, so update text to reflect it's already been mentioned, but this is a different usecase.
3+
In [exercise 21](/week-3/21-dietary), you used `<fieldset>` and `<legend>` to group checkboxes together. In this exercise, you will use these tags to describe a group of fields that are related to each other.
44

5-
The `<fieldset>` element is used to group together other form elements that are expected to be "related" to each other.
5+
To send the t-shirt to your customer you need to know where they want it to be delivered. This may be different from the address on the card they use to make payment. In this exercise, you will create two groups of fields: one for the billing address and one for the delivery address.
66

7-
A `<fieldset>` element is normally by a `<legend>` element that provides a brief description for the grouping.
7+
1. Find the line in the `index.html` page where it says `<!-- Add the form fields here -->`.
8+
2. Below this line, add a `<fieldset>` with a `<legend>` that says "Billing Address".
9+
3. Add text input fields nested inside of the `<fieldset>`. Add fields for the following: Address Line 1, Address Line 2, Postcode and City.
10+
4. Repeat steps 2 and 3 for a set of fields named "Delivery Address".
811

9-
An example of this would be, in an online shop checkout, separating the delivery address fields and cardholder address fields onto two different fieldsets.
12+
_Hint: Every field needs a unique `name` attribute. You can't have two fields named `postcode`, so you will need to name each set differently._
1013

11-
```
12-
<form method="POST" action="/checkout">
13-
<fieldset name="cardholder-address">
14-
<legend>Cardholder address:</legend>
15-
<label for="cardholder-address-1">Address line 1:</label>
16-
<input type="text" id="cardholder-address-1" name="cardholder-address1" value required>
17-
<label for="cardholder-address-2">Address line 2:</label>
18-
<input type="text" id="cardholder-address-2" name="cardholder-address2" value>
19-
<label for="cardholder-postcode">Postcode:</label>
20-
<input type="text" id="cardholder-postcode" name="cardholder-postcode" value required>
21-
<label for="cardholder-town">Town:</label>
22-
<input type="text" id="cardholder-town" name="cardholder-town" value required>
23-
</fieldset>
14+
When you are done, it should look like this.
2415

25-
<fieldset name="delivery-address">
26-
<legend>Delivery address:</legend>
27-
<label for="delivery-address-1">Address line 1:</label>
28-
<input type="text" id="delivery-address-1" name="delivery-address1" value required>
29-
<label for="delivery-address-2">Address line 2:</label>
30-
<input type="text" id="delivery-address-2" name="delivery-address2" value>
31-
<label for="delivery-postcode">Postcode:</label>
32-
<input type="text" id="delivery-postcode" name="delivery-postcode" value required>
33-
<label for="delivery-town">Town:</label>
34-
<input type="text" id="delivery-town" name="delivery-town" value required>
35-
</fieldset>
36-
37-
<input type="submit" value="Submit">
38-
</form>
39-
```
40-
41-
The above example would look like this:
42-
43-
![Fieldset example](../../img/fieldset-example.png)
16+
![Screenshot of the billing and delivery address fieldsets](/images/23/solution.png)

week-3/24-attributes/index.html

+49
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,55 @@ <h2 class="checkout__title">Checkout</h2>
4040
<form method="GET" id="checkout-form" class="checkout__form">
4141

4242
<!-- Add the form fields here -->
43+
<fieldset>
44+
<legend>Billing Address</legend>
45+
<p>
46+
<label for="billing-address-1">Address Line 1</label>
47+
<input type="text" name="billing-address-1" id="billing-address-1">
48+
</p>
49+
<p>
50+
<label for="billing-address-2">Address Line 2</label>
51+
<input type="text" name="billing-address-2" id="billing-address-2">
52+
</p>
53+
<p>
54+
<label for="billing-postcode">Postcode</label>
55+
<input type="text" name="billing-postcode" id="billing-postcode">
56+
</p>
57+
<p>
58+
<label for="billing-city">City</label>
59+
<input type="text" name="billing-city" id="billing-city">
60+
</p>
61+
</fieldset>
62+
<fieldset>
63+
<legend>Delivery Address</legend>
64+
<p>
65+
<label for="delivery-address-1">Address Line 1</label>
66+
<input type="text" name="delivery-address-1" id="delivery-address-1">
67+
</p>
68+
<p>
69+
<label for="delivery-address-2">Address Line 2</label>
70+
<input type="text" name="delivery-address-2" id="delivery-address-2">
71+
</p>
72+
<p>
73+
<label for="delivery-address-postcode">Postcode</label>
74+
<input type="text" name="delivery-address-postcode" id="delivery-address-postcode">
75+
</p>
76+
<p>
77+
<label for="delivery-address-city">City</label>
78+
<input type="text" name="delivery-address-city" id="delivery-address-city">
79+
</p>
80+
<p>
81+
<label for="delivery-country">Country</label>
82+
<input type="text" name="delivery-country" id="delivery-country">
83+
</p>
84+
<fieldset>
85+
<legend>Delivery Options</legend>
86+
<label>
87+
<input type="radio" name="delivery-option" id="delivery-option" value="standard">
88+
Royal Mail
89+
</label>
90+
</fieldset>
91+
</fieldset>
4392

4493
<button type="submit">Complete Purchase</button>
4594
</form>

0 commit comments

Comments
 (0)