Q:

Trying to pass in a callback function fails

I am trying to create some functionality when a user clicks on an element on the webpage. The callback function executes as soon as the page is executed. It is only supposed to execute when the user clicks on an element.
Here is the code:
<!DOCTYPE html>
<html>
  <head>
    <title>Javascript Test</title>
    <script src="http://code.jquery.com/jquery-latest.pack.js"></script>
    <script>
      $("#clickMe").one('click', printThis("Hello All"));
      function printThis(msg) {
        console.log(msg);
      }
    </script>
  </head>
  <body>
    <div id="clickMe">Click me!</div>
  </body>
</html>

Thanks!

A:

That isn't actually passing the function, but instead it is evaluating the function and passing the result as the callback parameter (in this case, undefined).
Try this instead
<script>
  function printThis(msg) {
    console.log(msg);
  }
  $("#clickMe").one('click', function() {
     printThis("Hello All");
  });
</script>

A:

Don't invoke the callback. Pass an anonymous callback function that invokes the function you want.
function printThis(msg) {
    console.log(msg);
}

$("#clickMe").one('click', function() { printThis("Hello All") });

