Can I display different text when blinking with Javascript?

I need to display text that changes every few seconds within a single div.

var blink_speed = 1000; // every 1000 == 1 second, adjust to suit
var t = setInterval(function () {
    var ele = document.getElementById('myBlinkingDiv');
     ele.style.visibility = (ele.style.visibility == 'hidden' ? '' : 'hidden');
}, blink_speed);
<div id="myBlinkingDiv">Blink 0</div>
    <div id="myBlinkingDiv1">Blink 1</div>
    <div id="myBlinkingDiv2">Blink 2</div>
    <div id="myBlinkingDiv3">Blink 3</div>
    <div id="myBlinkingDiv4">Blink 4</div>
    <div id="myBlinkingDiv5">Blink 5</div>
    <div id="myBlinkingDiv6">Blink 6</div>
    <div id="myBlinkingDiv7">Blink 7</div>
    <div id="myBlinkingDiv8">Blink 8</div>
    <div id="myBlinkingDiv9">Blink 9</div>
    <div id="myBlinkingDiv10">Blink 10</div>

Unfortunately, my code is not functioning correctly. I'm having trouble implementing an array here.

Answer №1

You can achieve the same result without using multiple divs.

Instead, consider creating an array with all the text you want to display and utilize JavaScript to dynamically change it.

Take a look at this example: http://jsbin.com/napelobura/edit?html,js,output

<div id="text"></text>

var blink_speed = 1000;
var wordArray = ['One', 'Two', 'Three'];
var count=0;
var t = setInterval(function () {
    var ele = document.getElementById('text');

    ele.innerHTML = wordArray[count++];

   if(count===wordArray.length)
     count=0;

}, blink_speed);

The goal here is to change the content of the div every few seconds based on the blink_speed variable and loop through the array using the count variable.

Answer №2

To achieve what you've shared, you can use the following code snippet:

var blink_speed = 1000; // adjust to your desired speed in milliseconds
var element = document.getElementById('myBlinkingDiv');
var timer = setInterval(function () {
     var number = parseInt(element.innerHTML.replace(/\D/g, ""));
     if (number > 10) number = -1;
     element.innerHTML = "Blink " + ++number;
}, blink_speed);
<div id="myBlinkingDiv">Blink 0</div>

Feel free to customize the code to display any other text you prefer...

Answer №3

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <script>
       function begin() {
           var count = 0;
           setInterval(function () {
               document.getElementById('blinkingElement').innerHTML="Blinking " + count;
               count = count + 1;        
           },1000);
       }
   </script>

</head>
<body>
    <input type="button" onclick="begin();" value="Begin"/>
  <div id="blinkingElement"></div>
</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve information from XML using jQuery

<?xml version="1.0" encoding="UTF-8"?> <slider> <csliderData1> <title>Kung Fu Panda</title> <content>In the Valley of Peace, Po the Panda finds himself chosen as the Dragon Warrior despite</content ...

Conceal and reveal submenu options

I have been exploring a jQuery function that toggles between hiding and showing different divs when a menu item is clicked. However, I am facing an issue where clicking on the same menu item does not close the newly opened div as intended. Additionally, I ...

Is it feasible to duplicate only the content of an array?

I attempted to create a duplicate of a 2-D array by using assignment, dup, and clone methods. @matrix = Array.new(3) { Array.new(3) } new_matrix = @matrix.clone However, any modifications made to the new variable also affected the original array. new_ma ...

The if-else statement is providing a misleading outcome

While working on my map using leaflet, I decided to implement a dynamic color concept based on input data. However, despite comparing 3 sets of data to ensure accuracy, some parts of the color scheme are displaying incorrect results. Below is the snippet o ...

Can the HTML title attribute be customized?

<h2 title="Site Owner">Mr. Mark </h2> Is there a way to customize the HTML title attribute using CSS, as shown in the example above? I attempted to apply inline CSS to it, but it didn't have any effect. ...

I must utilize the MongoDB native driver to retrieve unique IDs sorted by a timestamp column

I am currently utilizing the nodejs mongodb native driver for developing a chat application. Within my database, I have a collection named dialog which contains fields for sessionId and dateCreated (timestamp). My objective is to retrieve a list of distinc ...

A regular expression in Javascript that can be used to identify at least one word starting with a number among multiple words, with a

Currently, I am utilizing JavaScript Regex to validate an input field based on specific conditions: The value must consist of only alphabets and numbers There should be a minimum of two words (more than two are allowed) Only one word can start with a num ...

Tips for avoiding the influence of the parent div's opacity on child divs within a Primeng Carousel

I'm struggling to find a solution to stop the "opacity" effect of the parent container from affecting the child containers. In my code, I want the opacity not to impact the buttons within the elements. I have tried using "radial-gradient" for multipl ...

Identifying and implementing page language in nextJS: Solving the ReferenceError "window is not defined" issue

I am currently working on developing a website that can automatically detect the user's country and set the language accordingly. In React, I usually achieve this by using window.navigator.language. Here is a snippet of the code: import * as pt from ...

What is the proper way to reference a property's value within another property of the same JavaScript Object?

Within a Gulp.js file (or any Javascript file), I have defined paths in a Javascript object: var paths = { devDir : 'builds/development/', devDirGlobs : this.devDir+'*.html' } I am attempting to reference the pro ...

The system is unable to accept the Object ID

I am encountering an issue with displaying the Object ID from MongoDB in the console in VS Code. I am trying to retrieve the Object ID for the user who is currently logged into my application. I have created a hidden input tag with an id of ObjectID. <! ...

Create a bookmark system on an HTML page by utilizing the existing heading tags and implementing Javascript

Looking to implement a bookmark system using the headings within my HTML document, based on heading hierarchy. Unfortunately, my attempts have not been successful so far. https://i.stack.imgur.com/CdXjw.png I envision my bookmarks looking something like t ...

JSON - When an Object's Value Matches

Within my JSON file, I have an object that contains an array of nested objects. I am looking for a way to trigger a function based on the value of a specific object. If the value matches a certain number, I want to display only those objects within a desig ...

The parameter is causing the Aggregate Function to malfunction

When fetching MongoDB data, I encountered an issue. Using the JavaScript parameter to retrieve data from MongoDB resulted in a null return, but manually typing it brought all the data without any problems. That part is working fine. const data = await Numb ...

Key factors to keep in mind when comparing JavaScript dates: months

Check the dates and determine if the enddate refers to the following month by returning a boolean value. Example startdate = January 15, 2020 enddate = February 02, 2020 Output : enddate is a future month startdate = January 15, 2020 enddate = January 2 ...

The presence of asynchronous JavaScript can lead to errors due to missing functions in JavaScript

I've encountered an issue with a jQuery script that I modified by adding an ASYNC attribute. Surprisingly, it functions correctly in Firefox but encounters intermittent failures in both Chrome and IE browsers. Here's the PHP code snippet respons ...

Ways to prevent decreasing the value below zero in ReactJS?

I have created two buttons, one for increasing and another for decreasing a counter value. However, when I click on the minus button, it should not display negative values. But in my case, when I click on the minus button (initially at zero), it shows -1, ...

Clicking within the text activates the dropdown menu, but clicking outside the text does not

My custom drop down menu is not functioning properly. When I click on the text, it successfully links to another place, but when I click beside the text, it does not link. Can you please help me identify what's wrong here? Your assistance would be gre ...

Struggling to successfully update a database using a combination of javascript and PHP

I have been attempting to update a database by utilizing JavaScript and PHP. Below is the code from my index.html: <!DOCTYPE html> <html> <head> <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"> ...

Implementing Hover Effect on Elements within Item template of GridView

I want to update the background color of GridView rows when the mouse hovers over them. It's working fine for columns within <boundfield>, but the background color of labels inside <itemtemplate> does not change on MouseHover. This is how ...