0

I've written the following piece of code following this suggestion.

if (window.jQuery) {
  jQuery("document").on('mouseenter', 'div', function(event) {
    alert(jQuery(this).text());
  });
  alert("ok");
} else {
  alert("no!")
}
<body>
  <div>aaa</div>
  <div>aaa</div>
  <div>aaa</div>
  <div>aaa</div>
  <div>aaa</div>
  <div>aaa</div>
  <div>aaa</div>
  <div>aaa</div>
  <div>aaa</div>
  <div>aaa</div>
  <div>aaa</div>
</body>

Why does no alert box appear?

Community
  • 1
  • 1
tic
  • 4,009
  • 15
  • 45
  • 86

2 Answers2

3

There is no $("document") (I mean <document>) element in your page, but a document DOM object (MDN Docs) therefore you need:

$(document)

without the ""

Also, place ideally the <script> tag before the closing </body> tag:

<!-- SCRIPT GOES (ideally) HERE -->
</body>

Another place to put script tags is inside <head>. Just don't use it mixed within your HTML like in the middle of the page... - or it will pause document parsing and act as render-blocking (since the JS engine will kick-in trying to see what you desired).

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
0

The problem was - jQuery("document"). You cannot have document with quotes.

Try this -

if (window.jQuery) {
$(document).on('mouseenter','div',function(event){alert(jQuery(this).text());});
alert("ok");
}
else {alert("no!")}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div>aaa</div>
<div>aaa</div>
<div>aaa</div>
<div>aaa</div>
<div>aaa</div>
<div>aaa</div>
<div>aaa</div>
<div>aaa</div>
<div>aaa</div>
<div>aaa</div>
<div>aaa</div>
</body>
Harsheet
  • 728
  • 9
  • 23
  • Ok I edited that and added the problem as well. But I answered your question before it was accepted as the solution by you. Thanks – Harsheet Apr 03 '17 at 22:51