WordPress.org

Ready to get started?Download WordPress

Forums

Custom jQuery Script (4 posts)

  1. hancoha
    Member
    Posted 5 years ago #


  2. hancoha
    Member
    Posted 5 years ago #


  3. hancoha
    Member
    Posted 5 years ago #

    I need help getting my jQuery script to work in WP. The script works fine outside of WP and I am not really sure why this is so.

    I've created a script that randomly changes backgrounds based on an array. Here is the code for the script:

    <script type="text/javascript">
    	$(document).ready(function() {
      		var randomImages = ['bg1','bg2','bg3','bg4','bg5'];
      		var rndNum = Math.floor(Math.random() * randomImages.length);
      		$("#rotate").css({ background: "url(assets/bg/" + randomImages[rndNum] + ".png) no-repeat top center" });
    	});
    </script>

    I know that it's not convention to use the script tags in WP so instead, I've used the wp_enqueue_script to call my custom script. I've also changes out $() for jQuery() because I know that $() is default for the scriptalicious library. However, I still can't get the script to work in WP. I've tried calling a simplified script to see if the location of my script worked. I used the following code

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

    This code worked fine. Can someone PLEASE help me? Much appreciated!

  4. stvwlf
    Member
    Posted 5 years ago #

    within WordPress, instead of using $ use jQuery

    Then it will work fine

Topic Closed

This topic has been closed to new replies.

About this Topic