change event is fired once for a select if dialog is escaped or clicked out side

Amro Hassaan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 17, 2016

I am trying to write this simple javascript logic to disable multiselect field if user chose specific options from another select field. This is a JIRA dialog2, so I am using their JQuery namespace (eg: AJS).
Problem is when i open the dialog and select the specific values from the select field the handler for the change event of the select field runs properly and the other multiselect field is disabled. However, the snippet is not called/doesn't run if the dialog got closed by either pressing escape button or clicking outside it, as opposed to closing it by clicking the 'close' button. I am not a js expert and would appreciate any help in this.
Thanks

(function ($) {
      $(function () {
        AJS.dialog2.on("show", function (e) {
            var targetId = e.target.id;
            var spinning = false;
            if (targetId == "sr-dialog") {
              //start of the part that fails
              //noticing that the change event for the searchtypeselect field is fired only when the dialog shows up first time and re shows again only if the dialog got closed by the 'close' button. If dialog escaped it doesn't fire anymore.
              var $searchTypeSelect = AJS.$("#select-searchtype");
              var $fieldMultiSelect = AJS.$("#field-multiselect");
              $searchTypeSelect.change(function (e) {
              if ($searchTypeSelect.val() == 'D' || searchTypeSelect.val() == 'S') {                
                $fieldMultiSelect.removeAttr('disabled');
                } else { 
                $fieldMultiSelect.attr('disabled', 'disabled').val('');
               }
              }).trigger('change');
             // end of the part that fails
              AJS.$(e.target).find("#submit-spinner-trigger").click(function (e) {
                 var number = AJS.$("input[type=radio]:checked","#topSearchForm").val();
                 var searchType = AJS.$("#select-searchtype option:selected","#topSearchForm").val();
                 var fields = [];
                 AJS.$("#field-multiselect :selected","#topSearchForm").each(function(i,selected){
                   fields[i] = $(selected).val();
                });
                 var since = AJS.$("#select-since option:selected","#topSearchForm").val();
                 if (!spinning) {
                   AJS.$('.button-spinner').spin();
                   spinning = true;
               }               
              });
            }
        });
    });
})(AJS.$);

1 answer

1 accepted

1 vote
Answer accepted
Peter Geshev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 17, 2016

I'd guess that the close button removes the dialog from the DOM, and clicking outside hides it. If you only hide the dialog and later you show a new dialog there will be multiple items with the same id's in the DOM (e.g.  #select-searchtype) and the AJS.$ calls (e.g. AJS.$("#select-searchtype")) will not work properly which will cause the behaviour you have observed.

Amro Hassaan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 17, 2016

So, is it better to ensure removing it from the DOM on clickoutside or escape?

Peter Geshev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 17, 2016

That, or you can try using classes and attaching events/handlers to the elements of the dialog you are currently displaying(e.g.  var $searchTypeSelect = $(e.target).find('.searchType'), or s.th. like that).

Amro Hassaan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 22, 2016

@Peter Geshev [Botron], Thanks for the help. attaching the handler solved the issues and now it is working properly.

Suggest an answer

Log in or Sign up to answer