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 Posts

Fade a Divi Image Module Edge into the Background

Want to create a stylish fade effect on your Divi image module—where one side fades smoothly into the background? With a bit of CSS, you can make any edge (or corner) of the image fade out: top, bottom, left, right, or even diagonally.Fade a Divi Image Module Edge...

Hide the Header and Footer in the Hello Elementor Theme

Removing the default header and footer from your Hello Elementor theme allows for a streamlined and distraction-free website design. This is especially useful when creating unique landing pages, full-width layouts, or custom headers and footers with a page builder. In...

Setting up the Divi Password Box Module

Setting up password protection on a page can help you control access to sensitive or private content in WordPress, allowing only authorized visitors to view certain sections. With the Divi Password Box module, you can replace the plain Divi password form with a fully...

Open Divi Social Media Icons in a New Tab

Ensuring your website's social media icons open in a new browser tab creates a seamless user experience and keeps visitors engaged with your site while allowing them to explore your social media presence. This approach prevents users from navigating away from your...

Set Hover Color for Menu Links in the Divi Menu Module

Customizing the hover color of menu links lets you enhance the user experience and maintain visual consistency with your website’s brand. By setting a specific hover color, you can guide visitors' attention and improve navigation feedback. In this guide we show you...

Random Posts