Nik Nik - 4 years ago 165
Javascript Question

Jquery upgrade - 1.8 to 2.1.4, $(window) click vs $(document) click

I recently updated my project with latest jquery version.

For jquery 1.8, following statement worked:

$(window).on('click', 'button[data-mynav]', function() {
console.log('hit');
}
);


But it stopped working with jquery 2.1.4, but if i use $(document) instead of $(window) it works, that is:

$(document).on('click', 'button[data-mynav]', function() {
console.log('hit');
}
);


Can anyone please explain, why this is happening?

Answer Source

It stop working already on 1.8.1

<button a>works</button>
<button b>fails</button>

<!-- working -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script>

  $(window).on('click', 'button[a]', function() {
    this.innerHTML = 'hit';
  });
  
  jQuery.noConflict(true);
  
</script>

<!-- not working -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script>

  $(window).on('click', 'button[b]', function() {
    this.innerHTML = 'hit';
  });
  
</script>

This is there changelog between 1.8.0 and 1.8.1

My guess is on bug #12383 git: afd717d

I don't know exactly why it stop working as they didn't mention any breaking changes

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download