Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to trigger event on windows load

i like to execute $('select[name="order_id"]').change() on windows load but is not doing anything

if i debug this is browser console i can see script is execute $('select[name="order_id"]').change() but nothing happen is not sending ajax request

if i manually change the select or paste in browser console $('select[name="order_id"]').change() everything work fine

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

any ideas how to fix it ? i also tried with .trigger('change') but is same issue

        window.addEventListener("load", function (e) {
      
      <?php if ($logged): ?>
      
      $('select[name="order_id"]').change()
      
      $('select[name="order_id"]').change(function(){
        var order_id = $(this).val();
        isSelectedOrder = true

        $.ajax({
              url: 'index.php?route=return/getProductsByOrderId',
              data: {order_id:order_id},
              dataType: 'json',
              type: 'POST',
              beforeSend: function(){

                .......
                .....
                ..
                .

        <?php endif; ?> 
        
      }) 

>Solution :

Your problem is that you trigger the change() before attaching the event, all you need to do is move the trigger of the change() to after you attach the event.

window.addEventListener("load", function (e) {
      <? php if ($logged): ?>
      
      $('select[name="order_id"]').on('change', function () {
            var order_id = $(this).val();
            isSelectedOrder = true

            $.ajax({
                url: 'index.php?route=return/getProductsByOrderId',
                data: { order_id: order_id },
                dataType: 'json',
                type: 'POST',
                beforeSend: function () {

                .......
                },
            });
        });

        $('select[name="order_id"]').change()

    <? php endif; ?> 
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading