0

I'm trying without success to call this function when a div (#schedule), initially set as disabled, is shown.

function enable_create_btn(){
    if($('#schedule').is(":visible")) {
        $("#create_btn").prop('disabled', false);
    }
    else {
        $("#create_btn").prop('disabled', true);
    }
}

Is it possible to trigger on show event and call this function when div is becomes visible without adding external plugins (like this solution ?)

revy
  • 647
  • 2
  • 10
  • 29
  • 1
    Please post a [mcve] – j08691 Feb 24 '17 at 17:50
  • It seems like you are looking for something like this [http://stackoverflow.com/questions/1397251/event-detect-when-css-property-changed-using-jquery#answer-1397500](http://stackoverflow.com/questions/1397251/event-detect-when-css-property-changed-using-jquery#answer-1397500) – psycho Feb 24 '17 at 17:54
  • This accomplishes your goal: http://stackoverflow.com/a/16462443/693275 – Rob M. Feb 24 '17 at 17:56
  • Possible duplicate of [jQuery event to trigger action when a div is made visible](http://stackoverflow.com/questions/1225102/jquery-event-to-trigger-action-when-a-div-is-made-visible) – Dexygen Feb 24 '17 at 18:11

1 Answers1

1

Try using a mutationObserver to detect changes to the visibility attribute:

  var target = $('#schedule');
  var observer = new MutationObserver(function(mutations) {
      mutations.forEach(function(){
             //do stuff here
      });

      observer.observe(target, {
             attributes: true
      });
  }

If you're the one controlling the visibility however, this solution is kind of overkill in my opinion. The better approach is to just react to the change in the same place where you change the visibility

Pabs123
  • 3,385
  • 13
  • 29