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?

Answers


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>

some time its better to move forward without caring anything, this is the time you better start using

$(document).on('click', '#selector', function() {

}
);

coz jquery is currently using and continue to use it.


Need Your Help

`pygame.mouse.get_pressed()` reports clicks when mouse is not clicked

python mouse pygame

I am programming an experiment with Pygame 1.9.2 on Python 2.7. In the experiment, I display an image and ask the user to click either the left mouse button or right mouse button based on a feature...

Getting PHPStorm working with XDebug

php apache xdebug phpstorm

I am trying to get PHPStorm to talk to Xdebug, and am having very little luck.