Skip to content

Instantly share code, notes, and snippets.

@EncodeTheCode
Created May 13, 2024 05:44
Show Gist options
  • Save EncodeTheCode/22d63356530e21413126a613f025eca5 to your computer and use it in GitHub Desktop.
Save EncodeTheCode/22d63356530e21413126a613f025eca5 to your computer and use it in GitHub Desktop.
<!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