Issue not having a selected default option while populating select with ajax

Mattew Trincanati

I'm having a problem not being able to have a default selected option when i fill the select box with ajax call, i have the value, but not a default one, I want that the default one will be the first value i get from the call;

my HTML is (we will work on select2):

<div class="containerdiv">
    <select class="select1" name="dettaglio1">
    </select> <select class="select2" name="dettaglio2">
    </select> <select class="select3" name="dettaglio3">
    </select>
</div>

while the ajax call is that (Comments are for you):

loadEvento : function() {

            $select2 = $('.select2');
            luoghi.saveLuogo();  //dont care about
            $
                    .ajax({
                        url : 'http://localhost:8080/Redman-Mock-Services/services/comboevento',
                        dataType : 'json',
//                        data: JSON.stringify(opzioniSelezionate.luogo),
                        success : function(data) {
                            console.log(data);
                            $select2.html('');
//                          SETTING 1st value as selected
                            var first_iteration = true;
                            $.each(data, function(key, val) {
                                var o = new Option(val,key);
                                  if (first_iteration) {
                      o.selected = true; // ALSO TRYED $select2.html(o);
                                      first_iteration = false;
                                  }
                                   $select2.append(o);
                            })
                        },
                        error : function(err) {
                            $select2.html('<option id="0">nessun dato disponibile</option>');
                            console.log(err);

                        }
                    });

        }

and my html after the call is(F12) :

<div id="select-4-button" class="ui-btn ui-icon-carat-d ui-btn-icon-right ui-corner-all ui-shadow">
  <span class="select2">&nbsp;</span>
  <select class="select2" name="dettaglio2">
  <option value="0">option1</option>
  <option value="1">option2</option>
  <option value="2">option3</option>
  <option value="3">option4</option
  ><option value="4">option5</option>
  </select></div>

Is the problem on span element? and why it's blank ?

Mattew Trincanati

Thank to Pugazh I modified his answer and came to this working solution:

 $("#select-4-button").children("span").html($('select[name=dettaglio2] option:first').text());

the full code:

loadEvento : function() {

            $select2 = $('.select2');
            luoghi.saveLuogo();  //dont care about
            $
                    .ajax({
                        url : 'http://localhost:8080/Redman-Mock-Services/services/comboevento',
                        dataType : 'json',
//                        data: JSON.stringify(opzioniSelezionate.luogo),
                        success : function(data) {
                            console.log(data);
                            $select2.html('');
//                          SETTING 1st value as selected
                            var first_iteration = true;
                            $.each(data, function(key, val) {
                                var o = new Option(val,key);
                                  if (first_iteration) {
                      o.selected = true; // ALSO TRYED $select2.html(o);
                                      first_iteration = false;
                                  }
                                   $select2.append(o);
                            })
                         // Select the default value once all the options are loaded
                              $("#select-4-button").children("span").html($('select[name=dettaglio2] option:first').text());
                        },
                        error : function(err) {
                            $select2.html('<option id="0">nessun dato disponibile</option>');
                            console.log(err);

                        }
                    });

        }

I answered because is more clear the solution, but thumbs up his answer and comments that helped me.

(I've thumbed up everyone that helped, thanks a lot) ;)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to make a <option> default selected for a ajax-filled <select> with JQueryMobile?

From Dev

Laravel Option Select - Default Issue

From Dev

Laravel Option Select - Default Issue

From Dev

Dojo Select - first option getting selected by default

From Dev

How to check select contains default selected option or not?

From Dev

Add class to "select" if default option is selected

From Dev

Populating select box with neighborhoods of the selected city using AJAX

From Dev

Select specific <option> from <select> while having a value from PHP

From Dev

Select option selected an not selected

From Dev

Issue populating ajax response into a div

From Dev

Issue with multiple selected index queries (select option menu)

From Dev

How to make <select> element not to choose first option as selected by default?

From Dev

Angular Material md select option group default selected values

From Dev

Form with disabled select option selected as default not sending anything to validate

From Dev

Remove default option onChange event of selected select element?

From Dev

Populating dropdown menu - selected option from server

From Dev

Having a placeholder in a Select option

From Dev

Make <select> option selected

From Dev

populating multiple fields based on value selected with ajax

From Dev

facing issue while populating/referencing the variable in shell

From Dev

Select sentence having a selected word in it

From Dev

Dynamic select option with Ajax, json and PHP keep option selected after submit

From Dev

Populating Columns Using SELECT Statements - Formatting Issue

From Dev

setting a select option by another select selected option

From Dev

How to set default option selected?

From Dev

Default Option not selected dropdown in angularjs

From Java

default select option as blank

From Dev

HTML <select> default option

From Dev

default setting for option select

Related Related

  1. 1

    How to make a <option> default selected for a ajax-filled <select> with JQueryMobile?

  2. 2

    Laravel Option Select - Default Issue

  3. 3

    Laravel Option Select - Default Issue

  4. 4

    Dojo Select - first option getting selected by default

  5. 5

    How to check select contains default selected option or not?

  6. 6

    Add class to "select" if default option is selected

  7. 7

    Populating select box with neighborhoods of the selected city using AJAX

  8. 8

    Select specific <option> from <select> while having a value from PHP

  9. 9

    Select option selected an not selected

  10. 10

    Issue populating ajax response into a div

  11. 11

    Issue with multiple selected index queries (select option menu)

  12. 12

    How to make <select> element not to choose first option as selected by default?

  13. 13

    Angular Material md select option group default selected values

  14. 14

    Form with disabled select option selected as default not sending anything to validate

  15. 15

    Remove default option onChange event of selected select element?

  16. 16

    Populating dropdown menu - selected option from server

  17. 17

    Having a placeholder in a Select option

  18. 18

    Make <select> option selected

  19. 19

    populating multiple fields based on value selected with ajax

  20. 20

    facing issue while populating/referencing the variable in shell

  21. 21

    Select sentence having a selected word in it

  22. 22

    Dynamic select option with Ajax, json and PHP keep option selected after submit

  23. 23

    Populating Columns Using SELECT Statements - Formatting Issue

  24. 24

    setting a select option by another select selected option

  25. 25

    How to set default option selected?

  26. 26

    Default Option not selected dropdown in angularjs

  27. 27

    default select option as blank

  28. 28

    HTML <select> default option

  29. 29

    default setting for option select

HotTag

Archive