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:

1jQuery(function($){
2 alert('hello');
3});
Then you would enter the following into the box:
1<script>
2jQuery(function($){
3 alert('hello');
4});
5</script>
6
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. Hello! Thanks for your rich contributions!
    I urgently need to know how I can configure so that the search only works if at least one word is entered, or rather 1 or 2 letters. And that it does not show the entire site or the entire category if you simply click on the search button if the field is empty. I don't want it to work with an empty field, if you click like this, it should show the 404 page. It will already have the message that you did not find anything because you did not type anything! or something like that! I hope you understand me.

    Reply
    • Hey Carlos, I think I understand. The process will depend a bit on your search box and / or how you've set up the search results page. Is there any chance you're able to provide a link to the search box in question to check it out? One option that might work for you is to make the search boxes required fields, like so:

      https://divibooster.com/prevent-empty-searches-in-the-divi-search-box/

      That will stop the user being able to submit the search form at all, but note that it only works if the form is completely empty – typing a single space would bypass it. Not sure if that's a concern to you or not?

      Cheers!

      Reply
  2. Hi Dan! Super helpful, Thank you

    Reply
  3. Hi Dan!
    Please help me out because I'm losing my head!

    I am trying to use some query (or javascript) to change an image when clicking a button (without reloading the page).

    Do you have any advise?

    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

Enable Automatic Looping for YouTube Videos in the Divi Video Module

Enabling automatic looping for YouTube videos in your Divi video module ensures that content plays seamlessly and continuously without interruption. This feature can enhance user engagement, highlight key information, or create dynamic visual effects on your website....

Hide Video Controls in Divi Video Module

Disabling YouTube video controls in the Divi Video Module helps create a seamless, distraction-free viewing experience for your site visitors. This approach is useful when you want full control over how your video content appears and is interacted with on the page, or...

Mute YouTube Videos by Default in the Divi Video Module

In the Divi Video Module, starting your YouTube videos muted by default can create a more user-friendly browsing experience on your webpage. It is particularly beneficial for reducing distractions, enhancing visitor engagement, and providing a seamless content preview...

Autoplay Videos in Divi Video Module

Enabling videos in your Divi video module to start playing automatically can enhance both the site's design and user engagement. To ensure full compatibility with browser requirements, such as Chrome's autoplay policy, it's often necessary to start your videos muted...

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....

Random Divi Posts