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

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