I know its the easiest question but can’t get the correct answer.
JavaScript
x
13
13
1
<div class="col-12 col-md-4 col-lg-5 mt10-xs">
2
<p>Status</p>
3
<div class="form-group FL mr20 w100-xs">
4
<div class="rsi-custom-select">
5
<select class="form-control" id="statustab_{{ $row['placementkey'] }}" class="select2-selecting" onchange="listingByStatus(this,'{{ $row['placementkey'] }}')">
6
@foreach($placementStatus as $pl)
7
<option @if($pl['placement_status_id'] == $row['statusid'] ) selected @endif value="{{$pl['placement_status_key']}}">{{$pl['placement_status']}}</option>
8
@endforeach
9
</select>
10
</div>
11
</div>
12
</div>
13
This is my onchange
function. In this How Can I get the previous Selected Value from my select box?
JavaScript
1
6
1
function listingByStatus(ths,key){
2
var thisSelect = $('#statustab_'+key).text();
3
var statusText = $( "#statustab_"+key+" option:selected" ).text();
4
var currentval = $( "#statustab_"+key+" option:selected" ).val();
5
}
6
Advertisement
Answer
Save the original value using data()
when the element gets focus:
JavaScript
1
5
1
$('.select2-selecting').on('focusin', function(){
2
console.log("Saving value " + $(this).val());
3
$(this).data('val', $(this).val());
4
});
5
And then get the saved old value in your onchange
function:
JavaScript
1
8
1
function listingByStatus(ths,key){
2
var thisSelect = $('#statustab_'+key).text();
3
var statusText = $( "#statustab_"+key+" option:selected" ).text();
4
var currentval = $( "#statustab_"+key+" option:selected" ).val();
5
var prev = $('.select2-selecting').data('val'); //old value
6
console.log("Old value " + prev);
7
}
8