26th Oct '22
/
1 comment

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

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:
Auto-switching Bricks Tabs

Auto-switching Bricks Tabs

How to switch tabs every x seconds when the tabs element is scrolled into view.
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
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:
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:
McDonald’s Order Promise Analogy

McDonald’s Order Promise Analogy

From the JavaScript course I am doing now: A Promise is like placing an order at a restaurant. You receive a ticket (Promise object) that…
Categories:
How to disable smooth scroll in Bricks

How to disable smooth scroll in Bricks

In certain situations like using ScrollSmoother, you may be looking to disable smooth scrolling functionality that’s built into Bricks.
Categories:
How to Create a Grid Slider in Bricks

How to Create a Grid Slider in Bricks

In this tutorial, we'll learn how to create multiple rows and columns inside the default nested slider of Bricks. Introduction A user recently asked in…
Categories: