Flipping through words to create dynamic text transformation

Is there a way to use Javascript to dynamically change the text in an H1 tag every 2 seconds without altering the HTML structure?

For example:

1) Made for agencies... 2) Made for start-ups... 3) Made for small businesses...

I want the words after "Made for" to be switched out programmatically using only Javascript and CSS.

Any suggestions or solutions are greatly appreciated!

Answer №1

To implement a specific interval for calling and executing a function, you can utilize the setInterval() method.

var items = ['agencies', 'start-ups', 'small businesses'];
var itemCounter = 0;

setInterval(changeH1, 2000);   // set interval every 2 seconds to call a function

function changeH1() {   // function to perform changes to h1
  let h1 = document.getElementById('someH1');
  h1.innerHTML = "Made for " + items[itemCounter % items.length];
  itemCounter++;
}

changeH1();   // execute it first so that the h1 is immediately populated upon page load
<h1 id="someH1"></>

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

Ways of accessing an array within an if statement

I have a dashboard with admin privileges for my application. In this dashboard, the admin can select a user from a dropdown list. Once a user is selected, I make an AJAX call to retrieve the user's data and store it in a variable named $result. Howeve ...

How to isolate a function in React when mapping data

I am currently working on mapping data as a repeater in my React project. However, I am facing an issue with isolating the opening function, specifically with an accordion component. As I continue to learn React, I want to ensure that each accordion operat ...

What could be causing the unresponsive hamburger button on my navbar?

As a beginner in HTML and Flask, I am attempting to create a navbar. However, I am encountering an issue with the hamburger button not functioning properly. When I resize the window smaller, the hamburger button appears but does not show the items like "Lo ...

What is the best way to customize the appearance of only one of two identical tables using HTML and CSS?

I have two tables that look identical: <html> <head> <style> td, th { text-align: left; padding: 28px; } </style> </head> <body> <table> <tr> ...

`Switching between two buttons: A guide`

Here is the codepin: https://jsfiddle.net/magicschoolbusdropout/dwbmo3aj/18/ I currently have a functionality in my code that allows me to toggle between two buttons. When I click on one button, content opens and then closes when clicked again. The same b ...

Tips for displaying a bar graph instead of a line graph in a High Stoch Chart

I am working on incorporating High Stock Chart into my project, but instead of using a line graph, I would like to display a bar graph. Is there a way to achieve this? JS Fiddle My ultimate goal is to create a graph similar to the one in the provided scr ...

How to assign a click event to dynamically generated HTML elements using jQuery

My goal is to attach an onclick event to a dynamically inserted element using jQuery However, I am facing an issue where the function does not execute. I would appreciate it if someone could help me determine why this example is not functioning correctly ...

Guide to populate Div content using JSON information

I am working with a JSON object that contains arrays, and my goal is to dynamically populate a div element. I have a dropdown menu (select option) that I want to fill with all the keys from my JSON data. Each key in my JSON has an array of data as its va ...

Ways to obtain HTML as plain text

Issue: Whenever I use the console.log(${ref.current}) method, it displays a text like [object HTMLTableElement] instead of the desired text <h1> bla bla bla</h1>. Objective: How can I retrieve the HTML value as a string in ...

A significant decrease in speed is noticeable when Raphael JS is utilized with a large number of rectangles with backgrounds

I am faced with a challenge involving a large collection of small rectangles (4K-5K) and I am looking to implement the sprite technique in order to assign them a background using just one image to avoid overwhelming the server with requests. When I opt fo ...

Replacing Bootstrap CSS with a custom stylesheet

Within my bootstrap.css file, the following styling is defined: .form-control { display: block; width: 100%; height: 34px; padding: 6px 12px; font-size: 14px; line-height: 1.428571429; color: #555555; ve ...

anticipating the completion of useEffect and subsequently retrieving object properties

I'm struggling with the following code: const [files, setFiles] = useState([]); const [greeting, setGreeting] = useState(0); const [menu, setMenu] = useState([]); const [options, setOptions] = useState([]); var customer_id = 1; useEffect(() => { ...

Setting a single value for several identifiers within a JSON object

I'm new to JSON and I'm curious if it's possible to have a name/value pair with multiple names. Essentially, I want to be able to search for either name and retrieve the same value without having to update the value in two different places. ...

Having trouble loading data.json file in React.js and jQuery intergration

Having a background in mobile development, I am relatively new to web development so please excuse any amateur questions. Currently, I am working on a react.js project using create-react-app (which utilizes Babel). I am following a tutorial that requires ...

Repeated Hover Effects on CSS Menu Icons

the webpage I need help with is (issue with general menu drop down) Hello I recently decided to enhance my menu by incorporating icons. I had successfully implemented a drop-down menu using only HTML and CSS, but when I attempted to add icons next to ea ...

Establishing a connection with MSSQL 2014 through Node.js

I've been grappling with this issue for quite some time and I just can't seem to figure it out. Here is the code snippet that I have been using: const sql = require('mssql/msnodesqlv8'); const pool = new sql.ConnectionPool({ server: ...

Tips for excluding the mention of the final index within a for loop when passing the index as a parameter for a function

I'm struggling with the code I've written: function(customSlider, images){ // create thumbs container var thumbContainer = $("<div></div>").addClass(defaultOptions.thumbsContainerClass); // add thumbs ...

Having trouble with connect-busboy not calling the callback when using NodeJS, AngularJS, and File Upload?

Click here for the sources I am currently working on implementing a NodeJS server that allows file uploads using connect-busboy from AngularJS with the help of angular-file-upload for later analysis. The issue I'm encountering is that the callback f ...

Tips for transferring data from an Ajax response object into an element

Everything is working smoothly with my ajax function. It successfully fetches multiple objects from the database, all of which have attributes like supplier_name and supplier_id. The response object then places them into the correct element on the web page ...

Exploring the contents of variables within the forEach iteration

One issue I've encountered involves accessing variables from a forEach loop that iterates over an array of objects, and trying to use those variables outside the loop. I attempted to declare variables and assign them values from the loop, but I found ...