Module 6 - Lesson 12

jQuery and Divi in Practice

In this lesson I’ll give you a few practical tips on working with jQuery in Divi. First, we’ll do a short reminder on where we can add jQuery code, and then I will share a few useful examples and things to consider when implementing jQuery in Divi.

Video Chapters

00:00 Introduction

00:31 Adding JS to Divi Theme Options

01:36 Adding JS inside a Code module

02:40 Adding JS through a child theme

04:02 jQuery(document).ready Syntax

04:39 Modifying Text strings

07:11 Using JS with Ajax-generated content

09:39 Simple jQuery Read more / Read less

11:49 Button Click event with preventDefault

14:19 Targeting multiple elements with the same function

18:43 Show/Hide based on CSS class toggle

24:59 Modifying HTML structure with each()

29:32 Styling a new HTML structure with CSS

Code used:

jQuery(document).ready(function($){
	function renameLinks() {
		$('.pagination .alignleft a').text('Previous Articles');
		$('.pagination .alignright a').text('Next Articles');
	}
	renameLinks();
	
	$(document).ajaxComplete(function(){
		renameLinks();
	});

	$('.dsa-more-content, .dsa-read-less').hide();
	$('.dsa-read-more').click(function(e){
		e.preventDefault();
		$(this).parent().siblings('.dsa-more-content').slideDown();
		$(this).parent().siblings('.et_pb_button_module_wrapper').find('.dsa-read-less').show();
		$(this).hide();
	});

	$('.dsa-read-less').click(function(e){
		e.preventDefault();
		$(this).parent().siblings('.dsa-more-content').slideUp();
		$(this).parent().siblings('.et_pb_button_module_wrapper').find('.dsa-read-more').show(); 
		$(this).hide();
	});

	$('.dsa-side-toggle').click(function(){
		$(this).toggleClass('opened');
		$('.dsa-side').toggleClass('opened');
	});

	$('#dsa-blog .et_pb_post').each(function(){
		$(this).children().not('.entry-featured-image-url').wrapAll('
'); }); $(document).ajaxComplete(function(){ $('#dsa-blog .et_pb_post').each(function(){ $(this).children().not('.entry-featured-image-url').not('.blog-wrapper').wrapAll('
'); }); }); });
.et_pb_section.dsa-side {
    left: -300px;
    transition: all .3s ease-in-out;
}
.et_pb_section.dsa-side.opened {
    left: 0;
}

.dsa-side-toggle.opened .et-pb-icon {
    transform: rotate(180deg);
}

#dsa-blog .et_pb_post {
    display: flex;
}
#dsa-blog .et_pb_post:nth-child(2n) {
    flex-direction:row-reverse;
}

.blog-wrapper {
    padding: 20px;
    background: #eee;
}

#dsa-blog .et_pb_post img {
    height: 100%;
    object-fit: cover;
}

#dsa-blog a.entry-featured-image-url {
    margin-bottom: 0;
}

Join the discussion

Share your thoughts about this lesson with fellow students!

Login to your Account

If you're a DSA Student, please log in to your account to access the course content.

Sorry, you don't have access to this content.

It looks like you're not enrolled in Divi Stylist Academy.