• Resolved brandonf

    (@brandonf)


    I am creating a theme and for some reason javascript works fine, but jquery will not work. jquery appears in source as dependency but I cannot actually use it.

    I am just testing the scripts using an alert box:

    This is the code in my js file called extrajs.js:

    $(document).ready(
    
    	alert("hello");
    	);

    The above jquery DOES NOT work.

    But the same action in javascript DOES work in the exact same file:

    function ready() {
    	alert("hello");
    	}
    
    	window.onload = ready;

    Is there something I’m doing wrong in the jquery code?

Viewing 2 replies - 1 through 2 (of 2 total)
  • What’s likely happening here is that WordPress loads jQuery in no-conflict mode by default, so the $ shortcut isn’t available. One thing that some authors do is wrap the entire script within an anonymous function and pass $ as an argument so the shortcut will be available:

    ( function( $ ) {
      ... code goes here ...
      ... the $ shortcut will be available within this function ...
    })(jQuery);
    Thread Starter brandonf

    (@brandonf)

    Okay Thank you! that worked perfectly.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘jquery NOT working but javascript is working’ is closed to new replies.