Get 2025 Templates Mega Bundle!$1000 worth of 19 Bootstrap HTML, Vue & React Templates + 3 Vector Sets for just $99
Get for 99$

select2 on change event


Hi !
I'm using select element with data-kt-select2="true" , but when I declare on change event in my page KTUtil.onDOMContentLoaded , the event will not be trigger . Anyone know how to fix it ? (I'm using metronic laravel template and use laravel mix).

This is my html and js code :

<select class="form-select form-select-solid fw-bolder" id="birthdate_y" name="birthdate_y" data-kt-select2="true" data-allow-clear="true" data-hide-search="true" >
<option value=""></option>
<option value="1">1</option>
</select>



KTUtil.onDOMContentLoaded(function () {
$("#birthdate_y").on("change", function(){
console.log("in");
});
});


please help~~~ thx QQ


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (5)


Hi,

Please try to use the Select Event API as shown below:


$("#mySelect2").on("select2:select", function (e) {
// Do something
});


Regards.


Your Support Matters!

We will highly appreciate your Metronic Review on Themeforest.
Please go to Themeforest Downloads page, select Metronic & leave your feedback.

still the same ... not working ...

but when i declare in the resources/assets/extended/js/layout/app.js
line: 420


KTUtil.onDOMContentLoaded(function() {
KTApp.init();
$("#birthdate_y").on("change", function(){
console.log("in");
});

});

that will work... I don't know why.



Hi,

Please make sure that the script you declared the code in is loaded on your page after other theme scripts. If you need any further help can u please provide a test link to your page ?

Regard.



Sorry test link not yet created, but i'm sure the code is loaded, thanks for your help.
I'm using scripts.bundle.js and plugins.bundle.js in metronic html template instead , and all event can work normally, but in this way I can't use mix js , it's a little pity , but the project is in a hurry , so I use this solution first , maybe someday go back to figure the question out .



Hi,

Noted, all the best with your project.

When you have time please have a look at using build tools and editing the code from the src folder and get it compiled to assets folder. Nowadays the building tools are key for rapid and efficient development.

For more info about using Metronic please check the Theme Documentation
and Video Tutorials

Regards.


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(