Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the previous/old value on the "change"/"changed.bs.select" event? #1404

Closed
andrius-senulis opened this issue Jun 2, 2016 · 10 comments
Milestone

Comments

@andrius-senulis
Copy link

andrius-senulis commented Jun 2, 2016

Hello,

I have a problem getting the previous/old value of the select element on the change (or changed.bs.select) event. What would be the way to do it?

I don't want to use tricks like the following since I have many select elements in one page and I want to use the same event for all of them:

var previous;

$("select").on('focus', function () {
    previous = this.value;
}).change(function() {
    alert(previous);
    previous = this.value;
});

I have tried setting the data attribute with the old value to the select element but for some reason it does not get updated with the jQuery data() function.

Thank you,
Andrius

@andrius-senulis
Copy link
Author

andrius-senulis commented Jun 2, 2016

Actually, the following trick may work out:

var previous_value;
$(".selectpicker").on('shown.bs.select', function(e) {
        previous_value = $(this).val();
}).change(function() {
    alert(previous_value);
    previous_value = $(this).val();
});

@stimko68
Copy link

Is there any solution for this yet? I tried @andrius-senulis suggestion above but it's not working for me. I also have multiple select boxes which are dynamically added to the page, and I need to be able to get the old and new values.

I've also tried creating a JS function that would set the previous value and set that to the onfocus attribute of the select, like so:

<select class="selectpicker" onfocus="setPreviousValue(this.value)">
    <option>1</option>
</select>
var previous;

function setPreviousValue(newValue) {
    previous = newValue;
}

The onfocus attribute appears to be ignored. According to the documentation there is a reference to oldValue when the changed.bs.select event is fired but I'm not sure how to access it: https://silviomoreto.github.io/bootstrap-select/options/#events

@stimko68
Copy link

Update:

So it looks like you can access the oldValue value like so:

var previous;

$('.selectpicker').on('changed.bs.select', function (event, clickedIndex, newValue, oldValue) {
    previous = oldValue;
});

For whatever reason this still isn't working for me, but that could be an issue on my part. Maybe this will help someone else though.

@tbruyelle
Copy link

Same situation here, I get only boolean values fromnewValue and oldValue. I thought they would contain the content of option[value].

@yigitozkavci
Copy link

Yes. I am also getting boolean values from newValue and oldValue parameters. Docs forward me to wrong path it seems: https://silviomoreto.github.io/bootstrap-select/options/

@Avlexz
Copy link

Avlexz commented Feb 20, 2017

Hi guys. Recently I've encountered the same problem and I solved it using "shown.bs.select" event:

var previous_val;
$('[id$="_position_selector"].shown select').on('shown.bs.select', function() {
    previous_val = $(this).val();
}).change(function() {
    //do your action here
});

Hope this helps! ;)

@GreggOD
Copy link

GreggOD commented Jun 20, 2017

Hi guys,

As a note, the newValue and oldValues are supposed to return true/false, its the way they want it.

If you like me and show/shown events dont fire (my reason is due to jquery and bootstrap being included more than once **dont ask me, company thing)

I did the following to disable selected options and re-enable them should they not be selected anymore. You can also do something similar to remove and re-add properties.

Great solution:

        $('.selectpicker').on('changed.bs.select', function () {
            // check the old value
            var wasSelected = $(this).attr('data-selected');
            
            if ( wasSelected != "" ){
               $('.selectpicker').not( $(this) ).find('[value="'+wasSelected+'"]').prop('disabled', false);
            }
            // disable the new value in all select pickers but this one
            $('.selectpicker').not( $(this) ).find('[value="'+$(this).val()+'"]').prop('disabled', true);
            
            // Save the new value
            $(this).attr( 'data-selected', $(this).val() );
            
            // Refresh the selectpicker to reflect updates
            $('.selectpicker').selectpicker('refresh');
        });

@caseyjhol caseyjhol added this to the v1.13.0 milestone Jul 12, 2017
@jhonkrave
Copy link

@andrius-senulis thanks!! it's working for me.

@caseyjhol
Copy link
Member

Released in v1.13.0! @andrius-senulis Please feel free to close this issue.

@Supposer
Copy link

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants