Home > Back-end >  .change() deprecated doesn't works with .on()
.change() deprecated doesn't works with .on()

Time:09-30

I have a list of jQuery code which has the same event .change() which is now deprecated in latest jQuery. I have replaced that event with .on(). It works fine but the default values which needs to be hidden initially is now visible. Here is the Old and New Code. What wrong am I doing?

Old Code

    jQuery(".my_logo").change(function () {
        jQuery(".title").slideUp();
        jQuery(".logo").slideUp();
        var selected_teaser = jQuery(".my_logo option:selected").val();
        jQuery("." selected_teaser).slideDown();
    }).change();

New Code

    jQuery(".my_logo").on('change',function () {
        jQuery(".title").slideUp();
        jQuery(".logo").slideUp();
        var selected_teaser = jQuery(".my_logo option:selected").val();
        jQuery("." selected_teaser).slideDown();
    });

Note: If I don't remove the change() from the last in the New Code then it works.

CodePudding user response:

I think you forgot to initially trigger the change - so just add an inital trigger to your eventlistener:

jQuery(".my_logo")
  .on('change',function () {
        jQuery(".title").slideUp();
        jQuery(".logo").slideUp();
        var selected_teaser = jQuery(".my_logo option:selected").val();
        jQuery("." selected_teaser).slideDown();
    })
  .trigger('change');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  • Related