question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Select2 trigger "select2:select"

See original GitHub issue

I have a script that does the following code:

select2input.on('select2:select', function(e, e2) {
            if (e.params !== undefined && e.params.data !== undefined) {
                div.html(e.params.data.unidade_medida_txt);
                anotherinput.val(FloatField.format(e.params.data.valor_compra)).trigger('change');
            }
        });

I need to manually call “select2: select” and pass the data “e.params”, how do?

Something like:

select2input.trigger('select2:select', ...pass params...);

Thank you.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

23reactions
bgiesbrechtcommented, Jun 16, 2018

Hey @invious, I found this thread while struggling with this myself.

Calling val and then change does not seem to trigger the select2:select, you have to fire it yourself. The doc for this is here.

This is how I did it:

// Construct my data to select
var data = {
  "id": my_val
};

// Set the value
$("#foo").val(my_val);

// Change the select2 control to update it visually
// (does not fire select2:select)
$("#foo").trigger("change");

// Manually fire the event with my data
// This DOES fire select2:select.
$('#foo').trigger({
    type: 'select2:select',
    params: {
        data: 
    }
});

HTH

2reactions
michaelvickersukcommented, Feb 17, 2020

Chaining trigger('change') with trigger('select2:select') allowed me to successfully call my select2:select event.

$('#mySelect2')
   .val('1')
   .trigger('change')
   .trigger('select2:select');
Read more comments on GitHub >

github_iconTop Results From Across the Web

Events | Select2 - The jQuery replacement for select boxes
Select2 will trigger a few different events when different actions are taken using the component, allowing you to add custom hooks and perform...
Read more >
Select2 auto trigger event change - Stack Overflow
I just need set the new selected option without trigger the change event. Any alternative to .select2("val", "x") while using select2 plugin?
Read more >
params object is not present when fire event using .trigger ...
- When you want to tell Select2 to update the state of the selected options, you should trigger the `change` event. - When...
Read more >
Public jQuery events - 1112
What events will Select2 trigger? ; change: Triggered whenever an option is selected or removed. ; select2:close: Triggered whenever the dropdown is closed....
Read more >
Select2 3.5.3
Select2 3.5.3. Select2 is a jQuery based replacement for select boxes. It supports searching, remote data sets, and infinite scrolling of results.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found