DEV Community

Cover image for Personalizing Your Website with Random Text Displays
Yordi Verkroost
Yordi Verkroost

Posted on

1

Personalizing Your Website with Random Text Displays

The "Where to next?" section of my website includes a small story in the footer that helps visitors to explore semi-hidden pages and go on an adventure (inspired by Manuel). In it, I mention my favorite types of coffee, connecting to my page on Buy me a coffee. If you've paid close attention to this section, you might have noticed that the coffee types are randomly selected each time the page loads.

Here’s how I achieved that:

The HTML

The structure begins with this simple snippet:

I’m a huge <span id="random-coffee">caffè latte</span> fan, by the way.
Enter fullscreen mode Exit fullscreen mode

The <span> element with the id random-coffee serves as the placeholder for displaying the coffee name. By default, it shows "caffè latte" in case JavaScript is disabled or unavailable.

The JavaScript

To make the magic happen, I added the following JavaScript code:

const coffees = ["caffè latte", "cappuccino", "flat white", "cortado", "latte macchiato"];
const randomCoffee = document.getElementById("random-coffee");
randomCoffee.innerText = coffees[Math.floor(Math.random() * coffees.length)];
Enter fullscreen mode Exit fullscreen mode

Here’s a breakdown of the script:

  1. Define the options:

    The first line creates a list called coffees, listing five types of coffee. You can customize this list to include your own favorites or even replace it with types of tea or other beverages.

  2. Locate the placeholder:

    The second line uses the function document.getElementById() to locate the <span> element in the HTML by its unique id, random-coffee.

  3. Randomize the selection:

    The third line picks a random coffee type from the coffees array using the function Math.random() and updates the text content of the <span> element with the selected coffee.

That’s It!

This simple setup dynamically displays a different coffee name each time the page is loaded. It adds a little bit of randomness and personality to my site.

And now, if you’ll excuse me, I’m off to enjoy a warm and cozy caffè latte!

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay