npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

repeating-text

v1.0.0

Published

Repeat Text

Downloads

6

Readme

Creating a text repeater using JavaScript is a relatively simple process that involves the use of a few key functions and concepts. In this guide, we'll walk through the steps to create a text repeater that allows users to input a string of text and a number of times to repeat it, then displays the repeated text on the page.

Step 1: Set up the HTML and CSS

First, we'll create the HTML and CSS for the text repeater. In this example, we'll create a simple form that allows users to input the text they want to repeat and the number of times they want to repeat it. Here's the HTML for the form:

<!DOCTYPE html>
<html>
  <head>
    <title>Text Repeater</title>
    <style>
      body {
        font-family: Arial, sans-serif;
      }
      label {
        display: block;
        margin-bottom: 10px;
      }
      input[type="text"] {
        width: 100%;
        padding: 10px;
        font-size: 16px;
        border: 1px solid #ccc;
        border-radius: 4px;
        box-sizing: border-box;
        margin-bottom: 20px;
      }
      input[type="number"] {
        width: 100%;
        padding: 10px;
        font-size: 16px;
        border: 1px solid #ccc;
        border-radius: 4px;
        box-sizing: border-box;
        margin-bottom: 20px;
      }
      button {
        background-color: #4CAF50;
        color: white;
        padding: 10px 20px;
        border: none;
        border-radius: 4px;
        cursor: pointer;
      }
      button:hover {
        background-color: #45a049;
      }
    </style>
  </head>
  <body>
    <h1>Text Repeater</h1>
    <form>
      <label for="text-input">Enter the text to repeat:</label>
      <input type="text" id="text-input" name="text-input">
      <label for="repeat-input">Enter the number of times to repeat:</label>
      <input type="number" id="repeat-input" name="repeat-input">
      <button type="button" id="repeat-button">Repeat Text</button>
    </form>
    <div id="result-container"></div>
    <script src="script.js"></script>
  </body>
</html>

This code creates a basic form with two inputs: one for the text to repeat and one for the number of times to repeat it. It also includes a button to initiate the repetition and a div to display the repeated text.

The CSS styles the form inputs and button to make them look nice and the title is just a heading tag. We've also included a script tag at the bottom of the body to reference our JavaScript file, which we'll create in the next step.

Step 2: Set up the JavaScript

Now that we have the HTML and CSS in place, we can create the JavaScript code that will handle the text repetition. Here's the code:

const textInput = document.getElementById('text-input');
const repeatInput = document.getElementById('repeat-input');
const repeatButton = document.getElementById('repeat-button');
const resultContainer = document.getElementById('result-container');
repeatButton.addEventListener('click', repeatText);
function repeatText() {
  const text = textInput.value;
  const repeatCount = repeatInput.value;
  let repeatedText = '';
  for (let i = 0; i < repeatCount; i++) {
    repeatedText += text;
  }
  resultContainer.innerHTML = Repeated text: ${repeatedText};
}

This code creates four variables to store references to the HTML elements we'll be using: the text input, the repeat input, the repeat button, and the result container. It then adds an event listener to the repeat button that will call the repeatText function when the button is clicked.

The repeatText function gets the value of the text input and the repeat input, then initializes an empty string to store the repeated text. It then uses a for loop to repeat the text the specified number of times and add it to the repeatedText variable.

Finally, the function sets the innerHTML of the result container to display the repeated text.

Step 3: Test and Refine

Now that we have our JavaScript code in place, we can test the text repeater to make sure it works as expected. Open the HTML file in a web browser and try entering some text and a number of repetitions, then click the "Repeat Text" button. The repeated text should be displayed in the result container below the form.

If everything works as expected, you can refine the design and functionality of the text repeater to suit your needs. For example, you could add validation to make sure the repeat input is a valid number an option to copy the repeated text to the clipboard.

Here's an updated version of the JavaScript code that includes validation for the repeat input and a button to copy the repeated text to the clipboard:

const textInput = document.getElementById('text-input');
const repeatInput = document.getElementById('repeat-input');
const repeatButton = document.getElementById('repeat-button');
const copyButton = document.getElementById('copy-button');
const resultContainer = document.getElementById('result-container');
repeatButton.addEventListener('click', repeatText);
copyButton.addEventListener('click', copyText);
function repeatText() {
  const text = textInput.value;
  const repeatCount = repeatInput.value;
  if (!Number.isInteger(+repeatCount) || +repeatCount < 1) {
    resultContainer.innerHTML = 'Please enter a valid number of repetitions.';
    return;
  }
  let repeatedText = '';
  for (let i = 0; i < repeatCount; i++) {
    repeatedText += text;
  }
  resultContainer.innerHTML = `Repeated text: ${repeatedText}`;
}
function copyText() {
  const textToCopy = resultContainer.innerText.trim();
  if (textToCopy) {
    navigator.clipboard.writeText(textToCopy);
    alert('Repeated text copied to clipboard!');
  }
}

This code includes a new variable to store a reference to the copy button and adds an event listener to call the copyText function when the button is clicked.

The repeatText function has been updated to include validation for the repeat input. If the repeat input is not a valid integer or is less than 1, the function sets the result container's innerHTML to display an error message and returns early.

The copyText function gets the text to copy from the result container's innerText, trims any extra whitespace, then uses the Clipboard API to write the text to the clipboard. It also displays an alert to let the user know the text has been copied.

With these updates, the text repeater is now more robust and user-friendly. Visit this link to see a working demo : https://text-repeater.com.

We walked through the steps to create a text repeater using JavaScript. We created a simple HTML form, added CSS styles to make it look nice, then wrote JavaScript code to handle the text repetition and user interaction.

We also refined the functionality to include validation for the repeat input and an option to copy the repeated text to the clipboard.By following these steps, you can create your own text repeater and customize it to suit your needs.