user6104636 user6104636 - 7 months ago 47
HTML Question

how to make newly selected html option the first choice?

This is a simple problem that I cannot solve. Needless to say, I'm new to javascript.

I have sorted a dropdown list in alphabetical order, except for the first option as it has the selected value. Now when a user chooses a different value, I want that choice to be the first option and the rest in alphabetical order.

Do I need to have a sort in javascript to do this, or is there a function to 'append to the top of the list'?

sort in html is twig for sorting. I'm not using a js function...


<div class = "dropdowns">
<select id = "dropdown">
<option selected> name </option>
{% for key, value in Informations|sort %}
<option value="{{ key }}"> {{ value }} </option>
{% endfor %}


// Set textfield input to selected dropdown option
var myTextBox = document.getElementById('Infos');
var myDropdown = document.getElementById('dropdown');

document.addEventListener("DOMContentLoaded", function(event) {
// $("#dropdown").val($("#dropdown option:select").val());
// myDropdown.value = myTextBox.value

myDropdown.onchange = function () {
myTextBox.value = this.value;
myDropdown.value = myTextBox.value


I hope this will definitely solve your need..

<script src=""></script>

        if(a.text == b.text)
          return 0;
        if(a.text > b.text)
          return 1;
          return -1;

      var hold = $('option:selected',this).detach();

  <div class = "dropdowns">
    <select id = "dropdown">
      <option selected> x</option>