Trigger calls infinite times in Drupal behaviors

102 Views Asked by At

I want to remove all the options except only one in the select box and trigger the onchange based on this last option. I see the trigger calls infinite times. Below is my codes:

<select id="payment-method-pmid" name="payment_method[pmid]" class="form-select required ajax-processed">
<option value="select">- Select -</option>
<option value="1">Collect on delivery</option>
<option value="3">PayPal Instant Payment Notification</option>
</select>

(function ($) {
    Drupal.behaviors.siteModule = {
      attach: function (context, settings) {
            $('#payment-method-pmid').find('option').not(':last').remove();
            $('#payment-method-pmid').val(3).trigger('change');
      }
    };
})(jQuery);
1

There are 1 best solutions below

0
On

I've re-written my codes as below and it done.

(function ($) {
    Drupal.behaviors.siteModule = {
      attach: function (context, settings) {
            if(document.readyState == 'interactive'){
                $('#payment-method-pmid').val(3).trigger('change');
            } 
        $('#payment-method-pmid').find('option').not(':last').remove();
      }
    };
})(jQuery);