Created
May 13, 2024 05:44
-
-
Save EncodeTheCode/22d63356530e21413126a613f025eca5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Update Dropdown List</title> | |
</head> | |
<body> | |
<select id="myDropdown"> | |
<option value="1">Option 1</option> | |
<option value="2">Option 2</option> | |
<option value="3">Option 3</option> | |
<option value="4">Option 4</option> | |
<option value="5">Option 5</option> | |
</select> | |
<script> | |
document.getElementById("myDropdown").addEventListener("change", function() { | |
var dropdown = document.getElementById("myDropdown"); | |
var selectedValue = dropdown.value; | |
// Remove the selected option | |
dropdown.remove(dropdown.selectedIndex); | |
// Insert it back at the top | |
dropdown.insertBefore(new Option("Option " + selectedValue, selectedValue), dropdown.firstChild); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment