Home >Web Front-end >JS Tutorial >How to retrieve values from all types of HTML Inputs in JavaScript
This blog explains how to use JavaScript to retrieve values from different input types in an HTML form by their id.
<input type="text"> <ul> <li> <strong>JavaScript Code</strong>: </li> </ul> <pre class="brush:php;toolbar:false"> const textValue = document.getElementById('textInput').value;
<input type="email"> <ul> <li> <strong>JavaScript Code</strong>: </li> </ul> <pre class="brush:php;toolbar:false"> const emailValue = document.getElementById('emailInput').value;
<input type="password"> <ul> <li> <strong>JavaScript Code</strong>: </li> </ul> <pre class="brush:php;toolbar:false"> const passwordValue = document.getElementById('passwordInput').value;
<input type="number"> <ul> <li> <strong>JavaScript Code</strong>: </li> </ul> <pre class="brush:php;toolbar:false"> const numberValue = document.getElementById('numberInput').value;
<input type="date"> <ul> <li> <strong>JavaScript Code</strong>: </li> </ul> <pre class="brush:php;toolbar:false"> const dateValue = document.getElementById('dateInput').value;
<input type="radio"> <ul> <li> <strong>JavaScript Code</strong>: </li> </ul> <pre class="brush:php;toolbar:false"> const radioValue = document.querySelector('input[name="radioInput"]:checked')?.value || null;
<input type="checkbox"> <ul> <li> <strong>JavaScript Code</strong>: </li> </ul> <pre class="brush:php;toolbar:false"> const checkboxValues = Array.from(document.querySelectorAll('input[type="checkbox"]:checked')).map(cb => cb.value);
<select>
const dropdownValue = document.getElementById('dropdownInput').value;
<textarea>
const textareaValue = document.getElementById('textareaInput').value;
Here is an example that retrieves values from all the input types in a form:
<input type="text">
const textValue = document.getElementById('textInput').value;
This documentation provides a clear reference for working with various input types in HTML and collecting their values using JavaScript.
The above is the detailed content of How to retrieve values from all types of HTML Inputs in JavaScript. For more information, please follow other related articles on the PHP Chinese website!