A php include (a form submission) prints this when the form inputs were valid.
if ( $valid ) {
echo "<script type='text/javascript'>
$(document).ready(function() {
console.log('success');
notificationOutput('success', 'Successfully subscribed');
});
</script>";
In my header.php I’m including jQuery.
However when doing the thing above my console tells me that it doesn’t know $. It doesn’t make a difference if I write jQuery(document).ready(…
What do I miss here? As said above the echo is inside an included file so the header with all its scripts is already on the page.
Thank you in advance.
This isnt my answer i just copied it in here JQuery – $ is not defined
credit goes to @Mike Trpcic
That error can only be caused by one of three things:
Your JavaScript file is not being properly loaded into your page
You have a botched version of jQuery. This could happen because someone edited the core file, or a plugin may have overwritten the $ variable.
You have JavaScript running before the page is fully loaded, and as such, before jQuery is fully loaded.
You should check the Firebug net panel to see if the file is actually being loaded properly. If not, it will be highlighted red and will say “404” beside it. If the file is loading properly, that means that the issue is number 2.
Make sure all javascript code is being run inside a code block such as:
$(document).ready(function () {
//your code here
});
This will ensure that your code is being loaded after jQuery has been initialized.
One final thing to check is to make sure that you are not loading any plugins before you load jQuery. Plugins extend the “$” object, so if you load a plugin before loading jQuery core, then you’ll get the error you described.