Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the previous selected value from a dropdown before onchange

I know its the easiest question but can't get the correct answer.

<div class="col-12 col-md-4 col-lg-5 mt10-xs">
     <p>Status</p>
     <div class="form-group FL mr20 w100-xs">
           <div class="rsi-custom-select">
                 <select class="form-control" id="statustab_{{ $row['placementkey'] }}" class="select2-selecting" onchange="listingByStatus(this,'{{ $row['placementkey'] }}')">
                     @foreach($placementStatus as $pl)
                        <option @if($pl['placement_status_id'] ==  $row['statusid'] ) selected @endif value="{{$pl['placement_status_key']}}">{{$pl['placement_status']}}</option>
                     @endforeach
                 </select>
            </div>
      </div>
</div>

This is my onchange function. In this How Can I get the previous Selected Value from my select box?

function listingByStatus(ths,key){
      var thisSelect = $('#statustab_'+key).text();
      var statusText = $( "#statustab_"+key+" option:selected" ).text();
      var currentval = $( "#statustab_"+key+" option:selected" ).val();
}
like image 858
Siva Ganesh Avatar asked Feb 08 '18 06:02

Siva Ganesh


1 Answers

Save the original value using data() when the element gets focus:

$('.select2-selecting').on('focusin', function(){
    console.log("Saving value " + $(this).val());
    $(this).data('val', $(this).val());
});

And then get the saved old value in your onchange function:

function listingByStatus(ths,key){
         var thisSelect = $('#statustab_'+key).text();
         var statusText = $( "#statustab_"+key+" option:selected" ).text();
         var currentval = $( "#statustab_"+key+" option:selected" ).val();
         var prev = $('.select2-selecting').data('val'); //old value
         console.log("Old value " + prev);
}
like image 176
Sapnesh Naik Avatar answered Oct 28 '22 23:10

Sapnesh Naik