-2

Im trying to start a function when the page is loaded by a onload event which i include in the body tag. My function needs the $(this) value. The problem here is that the $(this) will not work in the function. But it works when I use the onClick event any ideas why and how to fix it?

This don't work.

<body onload="infoPopUp($(this), 3);">

This does.

<body onclick="infoPopUp($(this), 3);">

2 Answers2

2

You can easily replicate the <body onload=""> and <body onclick=""> in jQuery:

$(function () {
    // your document has been loaded
    // infoPopUp($('body'), 3) can be used here
});
Kenny Thompson
  • 1,494
  • 12
  • 28
  • Sidenote, `load` and `domcontentloaded` (document ready) are technically different events though they should both work in theory. See this article http://stackoverflow.com/questions/2414750/difference-between-domcontentloaded-and-load-events as a reference... – War10ck Jun 05 '15 at 20:11
  • 1
    Didn't know about the `DOMContentLoaded` event. In most cases I would want to use `Load` anyway =]. Thanks for that! – Kenny Thompson Jun 05 '15 at 20:14
0

Body is in document.body + you forgot to close quotes.

<body onload="infoPopUp(document.body, 3);">
pavel
  • 26,538
  • 10
  • 45
  • 61
  • Your closing quote should be after the semicolon. – Robert Lee Jun 05 '15 at 23:05
  • @RobertLee: you're right, but why downvote for typo which do nothing bad? In JS the semicolon is optional in this case and in html will be ignored. It works in all cases: with semicolon before ", after " and without it. – pavel Jun 06 '15 at 05:18