Here's an example of how you can reiterate a JavaScript function that disables an input based on a selected value from a dropdown menu:
<!-- HTML code for dropdown menu and input -->
<select id="myDropdown">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<input type="text" id="myInput">
<!-- JavaScript code to disable the input -->
<script>
const dropdown = document.querySelector("#myDropdown");
const input = document.querySelector("#myInput");
function disableInput() {
if (dropdown.value === "2") {
input.disabled = true;
} else {
input.disabled = false;
}
}
dropdown.addEventListener("change", disableInput);
</script>
In this example, we first select the dropdown menu and input elements using the querySelector
method. We then define a function called disableInput
that checks if the selected value from the dropdown menu is equal to "2". If so, we disable the input element by setting the disabled
property to true; otherwise, we enable it by setting the property to false.
Lastly, we add an event listener to the dropdown menu that calls the disableInput
function whenever the user selects a new option.
Asked: 2023-05-06 02:12:36 +0000
Seen: 16 times
Last updated: May 06 '23