Skip to content
Advertisement

How do I initialize by default value with select2 and trigger ajax request(select2:select)?

I want the first element of the selectbox to be selected by default when my page loads. It is selected but does not make ajax request. I want it to trigger the ‘select2: select’ event. Only the selected part in selectbox changes, it doesn’t execute the ajax request. I want it to work like ‘select2: select’ when I assign the first element. So when I select the first element, it should be triggered in the ajax request.

JavaScript

Advertisement

Answer

You can do using change event this will get trigger when you do trigger('change') or use .trigger("select2:select") to trigger select2:select event.

Demo Code :

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement