Every designer has needed placeholder text at some point, and a lorem ipsum generator is a surprisingly good beginner project. It teaches arrays, random selection, string building, and the Clipboard API — and it produces a tool you will actually use.
The generator needs a word bank. Classic lorem ipsum starts with the same sentence every time, so keep that as the first sentence and build the rest from a pool of Latin-sounding words:
const WORDS = [
"lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing",
"elit", "sed", "do", "eiusmod", "tempor", "incididunt", "ut", "labore",
"et", "dolore", "magna", "aliqua", "enim", "ad", "minim", "veniam",
"quis", "nostrud", "exercitation", "ullamco", "laboris", "nisi",
"aliquip", "ex", "ea", "commodo", "consequat",
];
const FIRST_SENTENCE = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
A sentence is a random number of words from the bank, capitalised, with a full stop. A paragraph is a random number of sentences, with the first paragraph starting with the classic opening line:
function randomSentence() {
const count = 5 + Math.floor(Math.random() * 10);
const words = Array.from({ length: count }, () =>
WORDS[Math.floor(Math.random() * WORDS.length)]
);
const text = words.join(" ");
return text.charAt(0).toUpperCase() + text.slice(1) + ".";
}
function makeParagraph(first) {
const count = 3 + Math.floor(Math.random() * 4);
const sentences = Array.from({ length: count }, randomSentence);
if (first) sentences[0] = FIRST_SENTENCE;
return sentences.join(" ");
}
The controls are the interesting part of the UI: number of paragraphs, and a choice between paragraphs, sentences, or words. One generate function handles all three modes:
function generate() {
const amount = parseInt(document.getElementById("amount").value, 10) || 3;
const type = document.querySelector("input[name='type']:checked").value;
let output = "";
if (type === "paragraphs") {
output = Array.from({ length: amount }, (_, i) =>
`<p>${makeParagraph(i === 0)}</p>`
).join("");
} else if (type === "sentences") {
const sentences = Array.from({ length: amount }, randomSentence);
sentences[0] = FIRST_SENTENCE;
output = `<p>${sentences.join(" ")}</p>`;
} else {
output = `<p>${FIRST_SENTENCE}</p>`;
}
document.getElementById("output").innerHTML = output;
}
Copying to the clipboard is the feature that makes the tool usable. The modern Clipboard API is one line, with a fallback for older browsers:
async function copyText() {
const text = document.getElementById("output").innerText;
try {
await navigator.clipboard.writeText(text);
document.getElementById("copy-btn").textContent = "Copied!";
setTimeout(() => (document.getElementById("copy-btn").textContent = "Copy"), 1500);
} catch (e) {
const textarea = document.createElement("textarea");
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
textarea.remove();
}
}
Ways to extend it: add a "start with Lorem ipsum" toggle, add a word-count display, add a download-as-text-file button using a Blob, or add themed word banks — tech words, food words — for funnier placeholder text. The pattern here — random selection from a pool, composed into a structured output — is the same one behind password generators, name generators, and test-data builders.
Top comments (0)