Adding Select and Textarea Elements

Tutorial 3 of 5

1. Introduction

This tutorial is designed to help you add <select> and <textarea> elements to your HTML forms. These elements provide more diverse opportunities for users to input data into your website.

By the end of this tutorial, you'll:

  • Understand how to use the <select> and <textarea> HTML tags.
  • Be able to create dropdown lists with the <select> tag.
  • Be able to create multi-line text inputs with the <textarea> tag.

Prerequisites: Basic understanding of HTML and HTML forms.

2. Step-by-Step Guide

<select>

The <select> tag is used to create a drop-down list. The <option> tags nested inside the <select> tag define the available options in the list.

Example:

<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

<textarea>

The <textarea> tag is used to define a multi-line input control (a text area).

Example:

<textarea name="message" rows="10" cols="30">
The cat was playing in the garden.
</textarea>

3. Code Examples

<select>

Here's an example of a <select> element with different options:

<select name="carBrand">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

This code will create a drop-down list with car brands. The value attribute in the <option> tag is what gets sent as the form data.

<textarea>

Here's an example of a <textarea> element:

<textarea name="message" rows="5" cols="50">
Write your message here.
</textarea>

This code creates a text area for users to input a message. The rows and cols attributes define the visible width and height of the text area.

4. Summary

  • We've learned how to create a drop-down list using the <select> and <option> tags.
  • We've also learned how to create a multi-line text input using the <textarea> tag.

For further learning, you can explore how to handle the data from these form elements using server-side languages like PHP or Node.js.

5. Practice Exercises

  1. Create a form with a <select> element that lets users choose their favorite fruit.

Solution:

<select name="favoriteFruit">
  <option value="apple">Apple</option>
  <option value="banana">Banana</option>
  <option value="cherry">Cherry</option>
</select>
  1. Create a form with a <textarea> element that lets users write a short bio.

Solution:

<textarea name="bio" rows="5" cols="50">
Write your short bio here.
</textarea>
  1. Create a form with both a <select> and a <textarea> element. The <select> should let users pick a topic, and the <textarea> should let them write a message about that topic.

Solution:

<select name="topic">
  <option value="sports">Sports</option>
  <option value="music">Music</option>
  <option value="movies">Movies</option>
</select>

<textarea name="message" rows="5" cols="50">
Write your message about the selected topic here.
</textarea>

For further practice, try to handle the form data using a server-side programming language of your choice.