Element <input>

Frontend SELF EN
Level 8 , Lesson 3
Available

3.1 Element <input type="button">

The <input type="button"> element is used to create a button that doesn't have a default behavior. Unlike submit and reset buttons that perform certain actions when clicked, the button is often used in combination with JavaScript to execute specific tasks.

Example usage:

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

Key attributes:

1. Attribute value: sets the text that will be displayed on the button.

HTML
    
      <input type="button" value="Click Me">
    
  

2. Attribute onclick: defines a JavaScript function that will execute when the button is clicked.

HTML
    
      <input type="button" value="Click Me" onclick="myFunction()">
    
  
JavaScript
    
      function myFunction() {
        alert('Button clicked!');
      }
    
  

Example usages:

Calling a JavaScript function:

HTML
    
      <input type="button" value="Show Alert" onclick="showAlert()">
    
  
JavaScript
    
      function showAlert() {
        alert('This is an alert!');
      }
    
  

Changing element style:

HTML
    
      <input type="button" value="Change Color" onclick="changeColor()">
      <p id="text">This is a paragraph.</p>
    
  
JavaScript
    
      function changeColor() {
        document.getElementById('text').style.color = 'blue';
      }
    
  

3.2 Element <input type="checkbox">

The <input type="checkbox"> element is used to create checkboxes that allow users to select one or multiple options from the given choices. A checkbox can be toggled on or off.

Example usage:

HTML
    
      <label for="subscribe">Subscribe to newsletter:</label>
      <input type="checkbox" id="subscribe" name="subscribe" value="yes">
    
  

Key attributes:

1. Attribute name: sets the name of the form element that will be sent to the server with the data.

HTML
    
      <input type="checkbox" name="subscribe">
    
  

2. Attribute value: sets the value that will be sent to the server if the checkbox is checked.

HTML
    
      <input type="checkbox" name="subscribe" value="yes">
    
  

3. Attribute checked: determines if the checkbox should be checked by default.

HTML
    
      <input type="checkbox" name="subscribe" value="yes" checked>
    
  

Example usages:

Multiple checkboxes:

HTML
    
      <p>Select your interests:</p>
      <input type="checkbox" name="interest" value="sports">Sports<br>
      <input type="checkbox" name="interest" value="music">Music<br>
      <input type="checkbox" name="interest" value="movies">Movies<br>
    
  

Checkbox with pre-selected value:

HTML
    
      <input type="checkbox" name="terms" value="agree" checked> I agree to the terms and conditions
    
  

3.3 Element <input type="radio">

Radio Button Field (type="radio")

The <input type="radio"> element is used to create radio buttons that allow users to select only one option from a group. Radio buttons are grouped together by using the same name attribute value.

Example usage:

HTML
    
      <p>Choose your gender:</p>
      <input type="radio" id="male" name="gender" value="male">
      <label for="male">Male</label><br>
      <input type="radio" id="female" name="gender" value="female">
      <label for="female">Female</label><br>
    
  

Key attributes:

Attribute name: sets the name of the radio button group. All radio buttons with the same name will belong to one group, and the user can select only one of them.

HTML
    
      <input type="radio" name="gender" value="male">
      <input type="radio" name="gender" value="female">
    
  

Attribute value: sets the value that will be sent to the server if the radio button is selected.

HTML
    
      <input type="radio" name="gender" value="male">
    
  

Attribute checked: determines if the radio button should be selected by default.

HTML
    
      <input type="radio" name="gender" value="male" checked>
    
  

Example usages:

Group of radio buttons to select one option:

HTML
    
      <p>Choose a color:</p>
      <input type="radio" name="color" value="red">Red<br>
      <input type="radio" name="color" value="green">Green<br>
      <input type="radio" name="color" value="blue">Blue<br>
    
  

Radio button with pre-selected value:

HTML
    
      <input type="radio" name="subscription" value="monthly" checked>Monthly<br>
      <input type="radio" name="subscription" value="yearly">Yearly<br>
    
  
1
Task
Frontend SELF EN, level 8, lesson 3
Locked
Checkbox
Checkbox
1
Task
Frontend SELF EN, level 8, lesson 3
Locked
Radio Buttons
Radio Buttons
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION