How to change the text of an element based on breakpoints without creating duplicated content

How to change the text of an element based on breakpoints without creating duplicated content

In this tutorial, we’ll learn how to dynamically change the text of a basic text element based on the viewport width in Bricks.

Table of Contents

Introduction

Sometimes your client will ask you to display different text content on desktop and mobile. The easy approach is to create two different DOM elements and set different CSS display properties based on the media queries. While this approach can be valid in some particular cases, it may have some downsides:

  • If the content of both desktop and mobile is similar, it may be considered as duplicated content by Google and penalize your SEO.
  • It creates 2 different DOM elements instead of 1.

Let’s see a different approach used by other pagebuilders that doesn’t create duplicated content and use a single DOM element.

The JavaScript

Paste the following code to your page. My preferred method to enqueue my scripts is described in this article, but feel free to use the method you are the most comfortable with.

window.addEventListener('DOMContentLoaded', () => {

   // Query the elements that have data-content as attribute
   const els = document.querySelectorAll('[data-content]');

   // Stop the function if there is no elements found
   if (els.length < 1) return;

   // Set the media query
   const mediaQuery = window.matchMedia('(max-width: 768px)')

   // Function
   const replaceContent = (e) => {

      let content;

      // Loop inside each element
      els.forEach(el => {

         // Parse the content attribute
         const attr = JSON.parse(el.dataset.content);

         // Desktop text
         content = attr.desktop;

         // Check if the media query is true
         if (e.matches) {

            // if the media query is true, change the mobile text
            content = attr.mobile;

         }

         // Change the text
         el.textContent = content;
      })

   }

   // Resize
   mediaQuery.addEventListener('change', replaceContent);

   // On load
   replaceContent(mediaQuery);

})

Note that we set the breakpoint to 768px. You may change the value to the one that suits your needs.

The data attribute

Now let’s jump on the text element and create a data-content attribute:

Inside the value field, we’ll paste a JSON object with both the desktop and mobile text structured as followed:

{
   "desktop":"Hello World!",
   "mobile":"Hi World!"
}

And that’s it!

Get access to all 524 Bricks code tutorials with BricksLabs Pro

1 comment

  • I solved this problem by creating a pseudoelement (::before) for a title element "world." Then just set the content property to "hello" on the desktop and "hi" on mobile.

Leave your comment

 

Related Tutorials..

Pro
Grid.js in Bricks

Grid.js in Bricks

This Pro tutorial provides the steps to use Grid.js in a WordPress site powered by Bricks builder. Grid.js is a lightweight and performant JavaScript library…
Categories:
Enqueueing a JavaScript File in Bricks

Enqueueing a JavaScript File in Bricks

Bricks child theme's functions.php comes with code to enqueue (load) style.css on the front end. What if you want to load a custom JavaScript file…
Categories:
Pro
Switching Tabs on Hover in Bricks

Switching Tabs on Hover in Bricks

This Pro tutorial shows how to make the tabs of Tabs (Nestable) elements active on hover in Bricks. Step 1 Add one or more Tabs…
Categories:
How to sync two nestable sliders in Bricks

How to sync two nestable sliders in Bricks

In this tutorial, we'll learn how to sync two nestable sliders in Bricks: one will be the main slider and the other one will be…
Categories:
Pro
How to Link to a Specific Tab in Bricks

How to Link to a Specific Tab in Bricks

This Pro tutorial provides the JavaScript code that will automatically switch to the tab when using Nestable Tabs elements based on the "tab" URL parameter…
Categories:
Pro
Opening Submenus with Click Instead of Hover in Bricks

Opening Submenus with Click Instead of Hover in Bricks

This Pro tutorial provides the code for opening submenus with click instead of hover in Bricks. Step 1 Add this in child theme's style.css: Step…
Categories:
Opening RSS Block’s Links in New Tabs

Opening RSS Block’s Links in New Tabs

Looking to have links output by RSS Block (in the block editor) in new tab/window when using Bricks? Add this in your Bricks template/Page at…
Categories:
Setting Active Tab in Bricks’ Tabs (Nestable) Element

Setting Active Tab in Bricks’ Tabs (Nestable) Element

With Bricks' Nestable tab element, here's how we can set which tab is active by default.
Categories:
Pro
WP Grid Builder Filters inside Bricks’ OffCanvas

WP Grid Builder Filters inside Bricks’ OffCanvas

This Pro tutorial provides the steps to move WP Grid Builder's filters inside the Bricks' native Offcanvas on smaller viewport widths. Let's work with the…