Showing posts with label html. Show all posts
Showing posts with label html. Show all posts

Sunday, 23 February 2025

Detailed Guide to the HTML button Element: Attributes, Types, and Examples

The <button> element in HTML is used to create clickable buttons. It is used to submit forms, trigger JavaScript events, or perform other interactive tasks.

Syntax

<button type="button|submit|reset" name="name" disabled>Text or HTML content</button>

 

Types of Buttons

1. Submit Button

Submits the form data to the server.

 

<form action="/submit" method="POST">
    <input type="text" name="name" />
    <button type="submit">Submit</button>
</form>

 

2. Reset Button

Resets all form fields to their initial values.

 

<form>
    <input type="text" name="name" value="John" />
    <button type="reset">Reset</button>
</form>

 

3. Regular Button (Button Type)

Used for any general purpose, typically with JavaScript events.

<button type="button" onclick="alert('Button clicked!')">Click Me</button>

 

button-demo.html

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Button Example</title>
</head>
<body>
    <form action="https://httpbin.org/post" method="post">
        <label for="name">Name:</label><br>
        <input type="text" id="name" name="name" autocomplete="name">
        <br><br>
        
        <label for="email">Email:</label><br>
        <input type="email" id="email" name="email" autocomplete="email">
        <br><br>

        <label for="street">Street Address:</label><br>
        <input type="text" id="street" name="street" autocomplete="street-address">
        <br><br>

        <label for="password">Password:</label><br>
        <input type="password" id="password" name="password" autocomplete="new-password">
        <br><br>

        <button type="submit">Submit</button>
        <button type="reset">Clear</button>
    </form>
</body>
</html>

 

Above snippet generate below screen.

 


Type something, an click on Clear button to clear the entered data.

 

Differences Between <button> and <input type="button">

1.   The <button> element can contain HTML content like images or text, whereas <input type="button"> is self-closing and cannot contain HTML.

2.   <button> supports multiple types (button, submit, reset), while <input> requires specifying the type (button, submit, or reset).

 

formaction and formmethod of button element

The formaction and formmethod attributes of the <button> element provide additional control over form submission behaviour, specifically for buttons inside forms.

 

formaction Attribute

The formaction attribute specifies the URL where the form data will be sent when the button is clicked. This attribute overrides the action attribute of the <form> element for that particular button. It is especially useful when you have multiple buttons within a form that need to submit data to different URLs.

 

formmethod Attribute

The formmethod attribute specifies the HTTP method (GET or POST) that the form will use when the button is clicked. Like formaction, this attribute overrides the method attribute of the <form> element for that particular button.

 

Example values

1.   GET: Appends form data to the URL as query parameters (used for retrieving data).

2.   POST: Sends form data in the body of the request (used for submitting data).

 

formmethod-action.html 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>formmethod and formaction Example</title>
</head>
<body>
    <form action="https://httpbin.org/post" method="post">
        <label for="name">Name:</label><br>
        <input type="text" id="name" name="name" autocomplete="name">
        <br><br>
        
        <label for="email">Email:</label><br>
        <input type="email" id="email" name="email" autocomplete="email">
        <br><br>

        <label for="street">Street Address:</label><br>
        <input type="text" id="street" name="street" autocomplete="street-address">
        <br><br>

        <label for="password">Password:</label><br>
        <input type="password" id="password" name="password" autocomplete="new-password">
        <br><br>

        <button type="submit" formaction="https://httpbin.org/get" formmethod="GET">Submit using GET</button>
        <button type="submit">Submit</button>
        <button type="reset">Clear</button>
    </form>
</body>
</html>

 

Above snippet generate below screen.



 

 

Previous                                                    Next                                                    Home

Thursday, 20 February 2025

Understanding the HTML textarea Element: A Guide to Rows, Cols, and Essential Attributes

The <fieldset> and <legend> elements in HTML are used together to group related form controls and label them. They help to improve the structure and accessibility of forms by providing a clear division and description of different sections within a form.

The <legend> element is used to provide a caption or title for the content within the <fieldset>. It usually appears at the top of the grouped section and describes the group of form elements for better clarity.

 

fieldset.html 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>datalist Example</title>
  </head>

  <body>
    <form action="https://httpbin.org/post" method="POST">
      <fieldset>
        <legend>Personal Information</legend>

        <p><label for="name">Name:</label>
        <input type="text" id="name" name="name" required /></p>

        <p><label for="email">Email:</label>
        <input type="email" id="email" name="email" required /></p>
      </fieldset>

      <fieldset style="margin-top: 30px">
        <legend>Preferences</legend>

        <p><label for="contact">Preferred Contact Method:</label>
        <select id="contact" name="contact">
          <option value="email">Email</option>
          <option value="phone">Phone</option>
        </select></p>

        <p><label for="subscribe">Subscribe to newsletter:</label>
        <input type="checkbox" id="subscribe" name="subscribe" /></p>
      </fieldset>

      <input type="submit" value="Submit" style="margin-top: 20px"/>
    </form>
  </body>
</html>

Above snippet generate below screen.

 


The first <fieldset> groups the "Personal Information" fields (name and email). The <legend> describes that this section contains personal details. The second <fieldset> groups the "Preferences" fields (preferred contact method and subscription to a newsletter), with a relevant <legend>.


Previous                                                    Next                                                    Home

Wednesday, 19 February 2025

Organizing Forms with HTML Fieldset and Legend Elements

The <fieldset> and <legend> elements in HTML are used together to group related form controls and label them. They help to improve the structure and accessibility of forms by providing a clear division and description of different sections within a form.

The <legend> element is used to provide a caption or title for the content within the <fieldset>. It usually appears at the top of the grouped section and describes the group of form elements for better clarity.

 

fieldset.html 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>datalist Example</title>
  </head>

  <body>
    <form action="https://httpbin.org/post" method="POST">
      <fieldset>
        <legend>Personal Information</legend>

        <p><label for="name">Name:</label>
        <input type="text" id="name" name="name" required /></p>

        <p><label for="email">Email:</label>
        <input type="email" id="email" name="email" required /></p>
      </fieldset>

      <fieldset style="margin-top: 30px">
        <legend>Preferences</legend>

        <p><label for="contact">Preferred Contact Method:</label>
        <select id="contact" name="contact">
          <option value="email">Email</option>
          <option value="phone">Phone</option>
        </select></p>

        <p><label for="subscribe">Subscribe to newsletter:</label>
        <input type="checkbox" id="subscribe" name="subscribe" /></p>
      </fieldset>

      <input type="submit" value="Submit" style="margin-top: 20px"/>
    </form>
  </body>
</html>

Above snippet generate below screen.


 

The first <fieldset> groups the "Personal Information" fields (name and email). The <legend> describes that this section contains personal details. The second <fieldset> groups the "Preferences" fields (preferred contact method and subscription to a newsletter), with a relevant <legend>.

 

 

Previous                                                    Next                                                    Home

Enhancing User Input with the HTML datalist Element

The <datalist> element in HTML provides a list of predefined options to an associated <input> element. It allows users to either select from a list of options or enter their own custom input, making the input process more flexible and user-friendly. The <datalist> is particularly useful when there are suggestions but not strict requirements, offering a way to guide users without limiting their input.


Unlike a <select> element, users can either pick an option from the list or enter a new value not in the list.

 

datalist.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>datalist Example</title>
</head>

<body>
    <form action="https://httpbin.org/post" method="POST">
        <label for="browser">Choose your favorite browser:</label>
        <input list="browsers" id="browser" name="browser" />
        <datalist id="browsers">
            <option value="Chrome">
            <option value="Firefox">
            <option value="Edge">
            <option value="Safari">
            <option value="Opera">
        </datalist>

        <input type="submit" value="Submit">
    </form>
</body>

</html>

Above snippet generate below screen.



 

 

Previous                                                    Next                                                    Home

Monday, 17 February 2025

Grouping Options of dropdown for Better Usability using optgroup element

The <optgroup> element in HTML is used to group related <option> elements inside a <select> dropdown. This helps to organize large lists of options into categories, making it easier for users to find what they are looking for. Each <optgroup> has a label attribute that defines the group name, which is displayed as a heading above the grouped options.

When you have a large number of options, organizing them into groups provides a clearer structure, reducing user confusion and enhancing the overall form experience. Without groups, a long list of unrelated options can confuse users. Grouping them makes navigation and selection easier.

 

Syntax 

<select name="select_name" id="select_id" multiple size="number_of_visible_options">
  
  <optgroup label="Group Label 1">
    <option value="option_value1">Option 1</option>
    <option value="option_value2">Option 2</option>
    <!-- More options -->
  </optgroup>

  <optgroup label="Group Label 2">
    <option value="option_value1">Option 1</option>
    <option value="option_value2">Option 2</option>
    <!-- More options -->
  </optgroup>

</select>

 group-options.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Optgroup Example</title>
</head>
<body>
  <form action="https://httpbin.org/post" method="POST">
    <label for="vehicles">Choose a vehicle:</label>
    <select id="vehicles" name="vehicles">
      <!-- Group 1: Cars -->
      <optgroup label="Cars">
        <option value="volvo">Volvo</option>
        <option value="bmw">BMW</option>
        <option value="audi">Audi</option>
        <option value="mercedes">Mercedes</option>
        <option value="tesla">Tesla</option>
      </optgroup>

      <!-- Group 2: Motorcycles -->
      <optgroup label="Motorcycles">
        <option value="harley">Harley Davidson</option>
        <option value="ducati">Ducati</option>
        <option value="kawasaki">Kawasaki</option>
        <option value="yamaha">Yamaha</option>
        <option value="suzuki">Suzuki</option>
      </optgroup>

    <option value="ford">Ford</option>
    <option value="chevrolet">Chevrolet</option>
    <option value="ram">Ram</option>
    <option value="gmc">GMC</option>
    <option value="toyota">Toyota</option>

    </select>
    <br><br>
    <input type="submit" value="Submit">
  </form>
</body>
</html>

Above snippet generate below screen.

 


Previous                                                    Next                                                    Home