Horje

Tips (Total 12)


# Tips-1) What is HTML Form Elements

This chapter describes all the different HTML form elements.

The HTML <form> element can contain one or more of the following form elements:

 


# Tips-2) How to create HTML <input> Element

One of the most used form elements is the <input> element.

Simple Example of HTML <input> Element

 <label for="fname">First name:</label>
<input type="text" id="fname" name="fname"> 

Full Example of HTML <input> Element

index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<h2>The input Element</h2>

<form action="/action_page.php">
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname"><br><br>
  <input type="submit" value="Submit">
</form>

</body>
</html>

Output should be:

Full Example of HTML <input> Element

# Tips-3) How to create The <label> Element

The <label> element defines a label for several form elements.

The <label> element is useful for screen-reader users, because the screen-reader will read out loud the label when the user focus on the input element.

The <label> element also help users who have difficulty clicking on very small regions (such as radio buttons or checkboxes) - because when the user clicks the text within the <label> element, it toggles the radio button/checkbox.

The for attribute of the <label> tag should be equal to the id attribute of the <input> element to bind them together.


# Tips-4) How to create HTML <select> Element

The <select> element defines a drop-down list:

Full Example of <select> Element

The select element defines a drop-down list:
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<h2>The select Element</h2>

<p>The select element defines a drop-down list:</p>

<form action="/action_page.php">
  <label for="cars">Choose a car:</label>
  <select id="cars" name="cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="fiat">Fiat</option>
    <option value="audi">Audi</option>
  </select>
  <input type="submit">
</form>

</body>
</html>

Output should be:

Full Example of <select> Element

# Tips-5) How to create HTML <option> element

The <option> element defines an option that can be selected.

By default, the first item in the drop-down list is selected.

To define a pre-selected option, add the selected attribute to the option:

Simple Example of Option Element

<option value="fiat" selected>Fiat</option>

Full Example of Option Element

You can preselect an option with the selected attribute:
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<h2>Pre-selected Option</h2>

<p>You can preselect an option with the selected attribute:</p>

<form action="/action_page.php">
  <label for="cars">Choose a car:</label>
  <select id="cars" name="cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="fiat" selected>Fiat</option>
    <option value="audi">Audi</option>
  </select>
  <input type="submit">
</form>

</body>
</html>

Output should be:

Full Example of Option Element

# Tips-6) How to Visible HTML Select Option Values

Use the size attribute to specify the number of visible values:

Simple Example of Visible HTML Select Option Values

Example: HTML
 <label for="cars">Choose a car:</label>
<select id="cars" name="cars" size="3">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="fiat">Fiat</option>
  <option value="audi">Audi</option>
</select> 

Full Example of Visible HTML Select Option Values

Visible Option Values Use the size attribute to specify the number of visible values.
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<h2>Visible Option Values</h2>

<p>Use the size attribute to specify the number of visible values.</p>

<form action="/action_page.php">
  <label for="cars">Choose a car:</label>
  <select id="cars" name="cars" size="3">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="fiat">Fiat</option>
    <option value="audi">Audi</option>
  </select><br><br>
  <input type="submit">
</form>

</body>
</html>

Output should be:

Full Example of Visible HTML Select Option Values

# Tips-7) How to Allow Multiple Selection in HTML Option Value

Use the multiple attribute to allow the user to select more than one value:

Simple Example of Allowing Multiple Selections:

<label for="cars">Choose a car:</label>
<select id="cars" name="cars" size="4" multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="fiat">Fiat</option>
  <option value="audi">Audi</option>
</select>

Full Example of Allowing Multiple Selections:

Allow Multiple Selections Use the multiple attribute to allow the user to select more than one value.
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<h2>Allow Multiple Selections</h2>

<p>Use the multiple attribute to allow the user to select more than one value.</p>

<form action="/action_page.php">
  <label for="cars">Choose a car:</label>
  <select id="cars" name="cars" size="4" multiple>
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="fiat">Fiat</option>
    <option value="audi">Audi</option>
  </select><br><br>
  <input type="submit">
</form>

<p>Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.</p>

</body>
</html>

Output should be:

Full Example of Allowing Multiple Selections:

Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.

# Tips-8) How to create HTML <textarea> Element

The <textarea> element defines a multi-line input field (a text area):

Simple Example of HTML textarea

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

Full Example of HTML textarea

The textarea element defines a multi-line input field.
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<h2>Textarea</h2>
<p>The textarea element defines a multi-line input field.</p>

<form action="/action_page.php">
  <textarea name="message" rows="10" cols="30">The cat was playing in the garden.</textarea>
  <br><br>
  <input type="submit">
</form>

</body>
</html>

Output should be:

Full Example of HTML textarea

The rows attribute specifies the visible number of lines in a text area. The cols attribute specifies the visible width of a text area. This is how the HTML code above will be displayed in a browser: You can also define the size of the text area by using CSS:

How to set width and height of textarea

Styling Textarea Use CSS to change the size of the textarea:
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<h2>Styling Textarea</h2>

<p>Use CSS to change the size of the textarea:</p>

<form action="/action_page.php">
  <textarea name="message" style="width:200px; height:600px;">The cat was playing in the garden.</textarea>
  <br>
  <input type="submit">
</form>

</body>
</html>

Output should be:

How to set width and height of textarea

# Tips-9) How to create HTML <button> Element

The <button> element defines a clickable button:

Simple Example of HTML Button

 <button type="button" onclick="alert('Hello World!')">Click Me!</button> 

Full Example of HTML Button

The button Element
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<h2>The button Element</h2>

<button type="button" onclick="alert('Hello World!')">Click Me!</button>

</body>
</html>

Output should be:

Full Example of HTML Button

Note: Always specify the type attribute for the button element. Different browsers may use different default types for the button element.

# Tips-10) How to create HTML <fieldset> and <legend> Elements

The <fieldset> element is used to group related data in a form.

The <legend> element defines a caption for the <fieldset> element.

Full Example of HTML <fieldset> and <legend> Elements

Grouping Form Data with Fieldset The fieldset element is used to group related data in a form, and the legend element defines a caption for the fieldset element.
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<h2>Grouping Form Data with Fieldset</h2>

<p>The fieldset element is used to group related data in a form, and the legend element defines a caption for the fieldset element.</p>

<form action="/action_page.php">
  <fieldset>
    <legend>Personalia:</legend>
    <label for="fname">First name:</label><br>
    <input type="text" id="fname" name="fname" value="John"><br>
    <label for="lname">Last name:</label><br>
    <input type="text" id="lname" name="lname" value="Doe"><br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>

</body>
</html>

Output should be:

Full Example of HTML <fieldset> and <legend> Elements

# Tips-11) How to create HTML <datalist> Element

The <datalist> element specifies a list of pre-defined options for an <input> element.

Users will see a drop-down list of the pre-defined options as they input data.

The list attribute of the <input> element, must refer to the id attribute of the <datalist> element.

Simple Example of HTML <datalist> Element

index.html
Example: HTML
 <form action="/action_page.php">
  <input list="browsers">
  <datalist id="browsers">
    <option value="Edge">
    <option value="Firefox">
    <option value="Chrome">
    <option value="Opera">
    <option value="Safari">
  </datalist>
</form> 

Full Example of HTML <datalist> Element

The datalist element specifies a list of pre-defined options for an input element.
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<h2>The datalist Element</h2>

<p>The datalist element specifies a list of pre-defined options for an input element.</p>

<form action="/action_page.php">
  <input list="browsers" name="browser">
  <datalist id="browsers">
    <option value="Edge">
    <option value="Firefox">
    <option value="Chrome">
    <option value="Opera">
    <option value="Safari">
  </datalist>
  <input type="submit">
</form>

</body>
</html>

Output should be:

Full Example of HTML <datalist> Element

# Tips-12) How to create HTML <output> Element

The <output> element represents the result of a calculation (like one performed by a script).

Simple Example of HTML <output> Element

Perform a calculation and show the result in an <output> element:
index.html
Example: HTML
<form action="/action_page.php"
  oninput="x.value=parseInt(a.value)+parseInt(b.value)">
  0
  <input type="range"  id="a" name="a" value="50">
  100 +
  <input type="number" id="b" name="b" value="50">
  =
  <output name="x" for="a b"></output>
  <br><br>
  <input type="submit">
</form>

Full Example of HTML <output> Element

The output element represents the result of a calculation.
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body>

<h2>The output Element</h2>
<p>The output element represents the result of a calculation.</p>

<form action="/action_page.php"
oninput="x.value=parseInt(a.value)+parseInt(b.value)">
  0
  <input type="range" id="a" name="a" value="50">
  100 +
  <input type="number" id="b" name="b" value="50">
  =
  <output name="x" for="a b"></output>
  <br><br>
  <input type="submit">
</form>

</body>
</html>

Output should be:

Full Example of HTML <output> Element