Select
To integrate the <input> element with a datalist (<datalist>) and a <select> dropdown menu within a <form>.

<form action="/submit-your-form" method="POST">
<!-- Input with Datalist -->
<label for="datalistInput">Choose a city:</label>
<input
list="datalistOptions"
id="datalistInput"
name="city"
placeholder="Type to search..."
autocomplete="off"
/>
<datalist id="datalistOptions">
<option value="San Francisco"></option>
<option value="New York"></option>
<option value="Seattle"></option>
<option value="Los Angeles"></option>
<option value="Chicago"></option>
</datalist>
<br><br>
<!-- Select Dropdown -->
<label for="selectOption">Choose an option:</label>
<select class="form-select" id="selectOption" name="option" aria-label="Default select example">
<option selected disabled>Open this select menu</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<br><br>
<!-- Submit Button -->
<button type="submit">Submit</button>
</form>
Explanation:
Form Element: The
<form>tag wraps around the input elements (<input>and<select>) and the submit button (<button type="submit">Submit</button>).Input with Datalist:
<input>: This is where users can type to search for options.list="datalistOptions": Associates the<input>with the<datalist>element by referencing itsid.autocomplete="off": Disables browser autocomplete for this input field.<datalist>: Contains the options that users can choose from when typing in the<input>field.
Select Dropdown:
<select>: Provides a dropdown menu with selectable options.class="form-select": Bootstrap class for styling purposes.aria-label="Default select example": Provides an accessible label for screen readers.<option>: Defines the selectable options within the dropdown. Theselectedattribute marks the default selected option.
Submit Button:
<button type="submit">Submit</button>: When clicked, this button submits the form to the server specified in theactionattribute of the<form>tag.
Attributes and Labels:
for="datalistInput"andfor="selectOption": Associates labels with their respective form elements for accessibility.name="city"andname="option": Specifies the names of the form fields that will be submitted with their respective values.
This structure ensures that users can choose options either by typing in the <input> field with autocomplete suggestions from the <datalist>, or by selecting an option from the <select> dropdown menu. Adjust the action attribute in the <form> tag to the appropriate server endpoint where you want to handle the form submission data.
Last updated