How to change options of , not selecting a specific option. – sisve Dec 02 '17 at 08:00

  • Answer may not be relevant to the question, but I found my way here as I was looking for this answer, so it helped. – Garrett W. Apr 13 '22 at 20:28
  • 2

    Old school of doing things by hand has always been good for me.

    1. Clean the select and leave the first option:

          $('#your_select_id').find('option').remove()
          .end().append('<option value="0">Selec...</option>')
          .val('whatever');
      
    2. If your data comes from a Json or whatever (just Concat the data):

          var JSONObject = JSON.parse(data);
          newOptionsSelect = '';
          for (var key in JSONObject) {
              if (JSONObject.hasOwnProperty(key)) {
                  var newOptionsSelect = newOptionsSelect + '<option value="'+JSONObject[key]["value"]+'">'+JSONObject[key]["text"]+'</option>';
              }
          }
      
          $('#your_select_id').append( newOptionsSelect );
      
    3. My Json Objetc:

          [{"value":1,"text":"Text 1"},{"value":2,"text":"Text 2"},{"value":3,"text":"Text 3"}]
      

    This solution is ideal for working with Ajax, and answers in Json from a database.

    Toto
    • 71
    • 1
    • 4
    1

    If for example your html code contain this code:

    <select id="selectId"><option>Test1</option><option>Test2</option></select>
    

    In order to change the list of option inside your select, you can use this code bellow. when your name select named selectId.

    var option = $('<option></option>').attr("value", "option value").text("Text");
    $("#selectId").html(option);
    

    in this example above i change the old list of option by only one new option.

    BERGUIGA Mohamed Amine
    • 6,094
    • 3
    • 40
    • 38
    • 1
      Why should the OP try this? Please add an explanation of what you did and why you did it that way not only for the OP but for future visitors to SO. – Jay Blanchard Mar 24 '15 at 14:03
    0

    if we update <select> constantly and we need to save previous value :

    var newOptions = {
        'Option 1':'value-1',
        'Option 2':'value-2'
    };
    
    var $el = $('#select');
    var prevValue = $el.val();
    $el.empty();
    $.each(newOptions, function(key, value) {
       $el.append($('<option></option>').attr('value', value).text(key));
       if (value === prevValue){
           $el.val(value);
       }
    });
    $el.trigger('change');
    
    Roman Yakoviv
    • 1,614
    • 15
    • 19
    0

    Most approaches start by deleting all the existing options and re-populating the dropdown. However, I prefer to add and remove individual options to avoid altering the current selection, if any.

    <option></option>
    <option value="12151d81-fe94-4007-8f1f-907aa8129d0a" data-text="Scrambled Eggs">Scrambled Eggs</option>
    

    This example assumes that the options HTML is structured like the above. options is JSON data like the following:

    {
        "guid": "bf42ba25-02b3-4d49-b4cf-02467c4fc44d",
        "value": "Granola"
    },
    {
        "guid": "89004730-bb92-4cd6-986a-28e228a94d54",
        "value": "Yogurt"
    }
    

    field is the input object.

    function update_options(field, options) {
                    // If item doesn't exist in dropdown, add it.
                    $.each(options, function (index, item) {
                        if (field.find(`option[value="${item.guid}"]`).length == 0) {
                            $(`<option value="${item.guid}" data-text="${item.value}">${item.value}<\/option>`).appendTo(field);
                        }
                    });
                    // If item in dropdown doesn't exist in data, remove it.
                    $.each(field.find('option'), function (index, item) {
                        if (options.find(x => x.guid == item.value) == undefined) {
                            field.find(`option[value="${item.value}"]`).remove();
                        }
                    });
                }
    
    bendodge
    • 470
    • 5
    • 12