Adding JavaScript / jQuery to the Divi Theme

Written by Dan Mossop

There are several ways to add JavaScript (or jQuery – basically just of JavaScript with some additional features) code to the Divi Theme. One of the easiest ways is via the Divi Theme Options area.

Adding JavaScript / jQuery via the Divi Theme Options

If you go to the "Divi > Theme Options > Integration" tab from your WordPress Dashboard, you'll see the following:

You can use the "Add code to the < head > of your blog" box to add JavaScript / jQuery:

This box is actually for entering HTML code, but you can tell it you want to add JavaScript or jQuery code by surrounding your code in <script> and </script> tags.

For example, if you have the following code:

jQuery(function($){
    alert('hello');
});
Then you would enter the following into the box:
<script>
jQuery(function($){
    alert('hello');
});
</script>
So you'd have:

Then just save the options and your code should run when you view / preview your site. In this example, the code will just pop up a message saying "hello" whenever someone loads a page on your site. If you'd like to try a more useful example, please see this post on how to Make the Divi accordions closed by default.

Run PHP Code Directly in Your Divi Layouts

Unlock endless customization and dynamic functionality by seamlessly adding PHP code to your Divi pages and posts with the Divi PHP Code Module. This powerful tool allows you to integrate complex features and automate processes directly in your Divi environment, enhancing the interactivity and capabilities of your site.

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

31 Comments

  1. As always, you're a genius. Thanks for your contributions to the Divi world!

    Reply
    • Ha ha! You're very welcome, Nate! :)

      Reply
  2. Hello!

    I have a doubt.

    Can I create a section in the divi and then (just below) insert a js effect into a code module in the divi?

    Or will I have to copy the html from that section and add it to the divi code module?

    Thanks!

    Reply
    • Hi Alessandra! It's certainly possible to have a section and then have a js effect for that section in a code module below. The js code would of course need to reference the section somehow, e.g. by using the section's CSS ID. If you have problems getting the code to work, feel free to share a link to the page (either here or via the contact form) and I'll see if I can help out. Thanks!

      Reply
  3. I'm using Sabai directory and apparently, there is a known issue with Divi and the directory. Their solution is to change a line within the custom.js file. Would the integration part be a place to change this or do I need to edit the js file as they suggest?

    Reply
    • Hi Kimberly, my guess would be that if they require a change to a specific line then it would need to go in the custom.js file as it'll probably be modifying existing code. Are you able to share a link to the solution? Depending on what the change is there might be a way to do it via the integration part (which would be better as it won't be overwritten by updates). Thanks!

      Reply
  4. Hey – I just used your script fix for this issue in Divi "Extra". It worked great! Thank you for posting it!

    Reply
    • Hey Marta, thanks for getting in touch. There are a few differences between Divi and the Divi Builder plugin that might account for it, or it might be a conflict with the theme you're using. The easiest way for me to track it down will be to see what you have in place now, so is there any chance you're able to send me a link to the page you're working on? Thanks!

      Reply
  5. Hi, I'm trying to use the following animations: http://tobiasahlin.com/moving-letters/ in a text module, on a Divi page. But I don't know how to make it works…
    Thank you for your help!
    Ale

    Reply
  6. Hello,
    Unhappily none of scripts are working for me. I doing everything like the same you said but it's just simply pasting it as a text. Can you make it work for me somehow? Thanks!

    Here is it:
    http://imgur.com/a/GGcY9

    Reply
    • Hi Erik, you seem to be doing everything right, but it looks like something on your site is modifying the code (essentially turning it into non-code). If you haven't already, try the code in the "add code to the body" box instead of the head. If that still doesn't work, I'd try disabling your plugins one at a time to see if one of these is affecting the code. If you're able to pinpoint a plugin that's responsible or just keep having the same problem, let me know. Thanks!

      Reply
  7. Somehow I am seeing jquery is not defined in browser console, after I added my jquery code to the head box… I also tried implementing a new js file which contains my code and enqueueing it via functions.php of my child theme with dependency of jquery handle. Still seeing the jquery not defined error…

    Any help?

    Reply
    • Hi Saskia, I'd check a couple of things… first, make sure the references to jQuery are capitalized correctly, i.e. "jQuery" with a capital "Q". Also, can you see if the page is actually including and correctly loading the jQuery library? If not, you'd need to look into why that is (though Divi itself should load it correctly, in the absence of any other errors). Failing all that, any chance you're able to send me a link to the site, or a sample of you code to take a look? Thanks!

      Reply
  8. this simply doesnt work.
    it keeps adding my script into body and keeps it commented out somehow.
    search for signup_btn in HTML and you will find the jquery script.

    Reply
    • Hi ilija, is this working for you now? When I click the signup button, "done!" gets logged to the console, suggesting the script is running correctly.

      Side note: I don't think it matters, but "jQuery(function ($) {…}" and "$( document ).ready(function() {..})" are essentially equivalent, I believe. So you don't actually need both.

      Reply
  9. Is there a way to embed js code into a post?

    Reply
    • Hey Adam, an easy way to do it is using the built-in Divi code module. You can just paste your js into the module's "Content" box, surround it with <script> tags and you should be good to go. The great thing about this is that you can (if you want to) save the module to the Divi library and reuse it on posts as needed.

      One thing to be aware of is that the js will be executed as soon as it is encountered on the page (i.e. probably before everything has loaded). If you want to wait until the page has loaded, you may want to wrap your code in the jQuery ready function, as described here:

      https://learn.jquery.com/using-jquery-core/document-ready/

      Reply
      • Hello
        I have tried this and it wont work.
        i have divi3
        would you have any suggestions?

        Reply
        • Hey marie, it should still work in divi3. Any chance you're able to send me a link to the page you're working / let me know what code you're trying to add? Cheers!

          Reply
  10. Nice, can you give us an example of the JS Jquery that can be done on this area?
    Thanks.

    Reply
    • I've just updated the post to give an example. Hope it helps.

      Reply
  11. Hi,
    Can you give an example of some usage of this? Thank you for your time, I appreciate it!
    -John

    Reply
    • Hi John, I've just updated the post with an example. Cheers!

      Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

Your email address will not be published. Required fields are marked *.

We may earn a commission when you visit links on our website.

Latest From Divi Booster

How to Add the Post Status in Divi's Dynamic Content

Divi's Dynamic Content feature is great for enhancing your post/page templates with post-specific information. While Divi includes an option to show information such as the Post Created Date using Dynamic Content, there is no equivalent option for the Post Status....

Fix Divi Text Module "Regular" Font Weight Not Working

Are you encountering an issue where the font weights in Divi's Text Module don't seem to apply as expected? In particular, you might find that when you set the text module's body text font weight to "Regular", your font is actually assigned a weight of 500 instead of...

Make the Divi Gallery Module Swipeable

The Divi Gallery Module comes with a slider mode that lets you display images in a slider with clickable left/right arrows to scroll through the images. For mobile / touchscreen users, you can improve the user experience by allowing your user to swipe to navigate to...

How to Use Divi Dynamic Content in Woo Modules Product Selector

Divi's Dynamic Content is a powerful feature that lets you populate your Divi modules and theme builder templates with data pulled in various sources, such as custom fields. This allows for efficient organization and maintenance of your sites.Unfortunately, Divi's...

Random Divi Posts