Running jQuery in the browser console

Hi there,

Given the following page:

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>A very boring page</title>
  </head>
  
  <body>
    <button id="myDiv">Click me!</button>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  </body>
</html>

If you open it in your browser (Chrome for me) and click on the button, nothing happens.

Then if you open the console by presisng F12 -> Console.
Then copy paste the following code:

> $("#myDiv").on("click", function(){ alert("Yay!"); });

Note: it can be on multiple lines, no problem.

You should see:

[<button id=&#8203;"myDiv">&#8203;Click me!&#8203;</button>&#8203;]

Now if you click on the button you will see an alert.

Note: You very probably don’t need the return true in the code you posted.