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 626 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 a Reply to Drew (Cancel Reply)

 

Related Tutorials..

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:
Pro
Post Title at a Different Location on Image Hover

Post Title at a Different Location on Image Hover

This Pro tutorial provides the steps to update the text of a sticky div with the title of the post that is hovered on the…
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
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:
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
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 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:
Pro
Button-specific HTML in Bricks Popup

Button-specific HTML in Bricks Popup

In the Bricks Facebook group a user asks: I need to create popups to expend the text (eg. read more). I wonder if I have…
Categories: