'Show/hide spinner on select change

I'm using LoadAwesome and I want to know how can I show spinner div when I change select value before load the data. Currently I'm using AJAX requests to load the data.

Simple explanation: selected change -> show spinner -> load data

I'll already try to do this, but data is loaded without the spinner effect. :(

<select id="select_data">
  <option value="">Choose one..</option>
  <option value="1">Value 1</option>
  <option value="2">Value 2</option>
</select>
<div class="content">
  <div id="spinner">
    <div style="color: #79bbb5" class="la-ball-spin-fade la-2x">
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
    </div>
  </div>
  <br>
  <div id="text"></div>
</div>

$("#select_data").change(function() {
    // I want to show the spinner when I change select value before load the data -->
   // So before show this, I'll show the spinner and then, the text.
   $("#text").html(($("#select_data option:selected").text()));
});

https://jsfiddle.net/0e6rtog5 - Unfortunatel jsfiddle doesn't show the spinner.



Solution 1:[1]

This will give gist of what you can do to load spinner on select change

$("#select_data").on('change', function() {
  // show spinner here
  $('#spinner').show(); // or do addClass('spinner')

  $.ajax({
    url: "test.html"
  }).done(function() {
    // hide again on response of ajax
    $('#spinner').hide(); // or remove class removeClass('spinner')
  });
});

DEMO

Please also add url for LoadAwesome module so that its easier for us

Solution 2:[2]

Use this:

Show on change:

 $('.la-ball-spin-fade').hide();//hide by default  -can use css
    $("#select_data").change(function({     
    $('.la-ball-spin-fade').show();
        //trigger ajax
    });

hide it at ajax success:

...
success:function(data) {
//other data manipulation;
 $('.la-ball-spin-fade').hide();
}

https://jsfiddle.net/mgp699za/1/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Raunak Kathuria
Solution 2