2

$(document).ready(function() {

  $('#dropdown select').on('change', function() {
    name = $('#dropdown select :selected').val();
    customerdetails(name);
  });

 


  


        //                $("#dropdown option").prop('selected', false).filter(function () {
        //                    return $(this).text() == 'hello';
        //                }).prop('selected', true);
        //                 $('<option/>').html(tempArray[0]).attr('selected', false);
        //$('#dropdown select').attr('selected', false);
      }


  });
 
<div id='dropdown' class="col-lg-3">
  <select class="form-control" id="Select1" name="dropdown">
                                     </select>
</div>

when DROPDOWNMENU is not selected, then get default value using jquery.

I just want first option of DROPDOWNMENU to be selected by default.

So before on change function, I just want 1st option selected by default

Ashish Bahl
  • 1,482
  • 1
  • 18
  • 27
ziel
  • 127
  • 2
  • 11
  • WHEN MY DROPDOWN IS NOT SELECTED THAN I WANT TO PAASS 1ST VALUE OF DROPDOWN INTO NAME – ziel Feb 22 '17 at 06:09
  • And when it is selected? – Rajesh Feb 22 '17 at 06:11
  • I just want 1st option selected by default – ziel Feb 22 '17 at 06:13
  • Let me clarify, do you want `name` attribute to update or you jjust want first value to be selected by default – Rajesh Feb 22 '17 at 06:14
  • JUST THE OPTION TO BE SELECTED BY DEFAULT @Rajesh – ziel Feb 22 '17 at 06:16
  • Possible duplicate of [How to make first option of – Rajesh Feb 22 '17 at 06:17
  • but by using this why I am getting [object][object] that default value on another page when I not selcting any option from dropdown – ziel Feb 22 '17 at 06:24

3 Answers3

1

If you want the first option to be selected by default you can do like this,

$('#Select1').val($("#Select1 option:first").val());


Or more easly by,

$("#Select1")[0].selectedIndex = 0;


After for (var i = 0; i < tempArray.length; i++) { .... }

Arun CM
  • 3,345
  • 2
  • 29
  • 35
0

please try this , it will select the first option

$(function(){

$('#dropdown select option:eq(0)').prop('selected', true);

})
Sooraz
  • 113
  • 7
0

I assume you want first item to be selected after loading options into Ur dropdown control. This can be achieved by using "option:first-child" selector. I have modified the code for clarity. Please check if that helps!!!

var arr = [];

var tempArray = [];
$(document).ready(function() {

  $('#dropdown select').on('change', function() {
    name = $('#dropdown select :selected').val();
    customerdetails(name);
  });

  namedetails();
});

function amcdetails() {

  $.ajax({
    async: true,
    type: "POST",
    url: "default.aspx/fetchname",
    data: '{}',
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    success: function(data) {
      var objdata = $.parseJSON(data.d);
      var i = 0;
      arr = [];
      arr = $.map(objdata.data, function(e, i) {
        var tmp = {
          0: e.NAME
        }

        return tmp;
      });


      for (var i = 0; i < arr.length; i++) {

        tempArray.push(arr[i][0]);

      }
      for (var i = 0; i < tempArray.length; i++) {
        $('<option/>', {
          value: tempArray[i],
          html: tempArray[i]
        }).appendTo('#dropdown select');

        $('#dropdown select option:first-child').attr("selected", "selected");

        //                $("#dropdown option").prop('selected', false).filter(function () {
        //                    return $(this).text() == 'hello';
        //                }).prop('selected', true);
        //                 $('<option/>').html(tempArray[0]).attr('selected', false);
        //$('#dropdown select').attr('selected', false);
      }

    },

    error: function(result) {
      alert(result.responseText);
    }

  });
}
<div id='dropdown' class="col-lg-3">
  <select class="form-control" id="Select1" name="dropdown">
                                     </select>
</div>
Shiva K Thogiti
  • 208
  • 2
  • 6