How to write on open event for select box using select2 in jquery. I tried using .on("change"), but it gets fired once we click the option inside the selectbox. I need the event which will get triggered the moment I open the selectbox
I tried with .click, .change, .focus
$('#js_pdp_campus').on('select2:open', function(e) {
console.log("hi");
let $campusData = "/product-commons/getcampusdata/?baseProductCode=MBS_86530";
$.ajax({
type: 'GET',
url: $campusData,
contentType: 'application/json',
success: function (data) {
console.log("poc data is", data);
},
error: function(error) {
}
});
return $campusData;
})
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/css/select2.min.css" rel="stylesheet" />
<script src="//code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/js/select2.min.js"></script>
<div class="control">
<select name="campus" id="js_pdp_campus" class="campusDataClass js-pdp-course-finder-select bned-select-input js-enable-select-input form-control js-basic-single" role="listbox">
<option>campus 1</option>
<option>campus 2</option>
<option>campus 3</option>
</select>
</div>
2
Answers
You can use events https://select2.org/programmatic-control/events
Use
.focus()
method for this. Try something like below.