skip to Main Content

Zup Stackoverflow!
I have a question regarding a problem I can’t seem to solve. I want to add text with a intervalfunction which should only be seen after the already existing text inside a h2 tag. The data comes from the list array. So something like this:

HTML:

<div id="output">
    <h2 id="funny-day">Funny day: (HERE IS WHERE I WANT THE OUTPUT TO BE)</h2>
</div>

Javascript:

//Get elements
var funny-day = document.getElementById("funny-day");

//Array
var list = ["Sunny", "Cloudy", "Rainy"]

//Function to output array
var index = 0;
markiser.innerHTML = markiser.textContent;
setInterval(function() { 

    var text = (list[index++ % list.length]).toString();
    markiser.innerHTML += text; <<Not working ofcourse since it only appends 

}, 2000);

Important note is that I want to keep the “Funny day: ” inside the h2 tag because of SEO purposes.

2

Answers


  1. Why don’t you add a tag specificly for your data like <span class="data">yourdata</span>

    //Get elements
    var funnyDay = document.getElementById("funny-day");
    
    //Array
    var list = ["Sunny", "Cloudy", "Rainy"]
    
    //Function to output array
    var index = 0;
    setInterval(function() { 
    
        var text = (list[index++ % list.length]).toString();
       document.getElementById("data").innerHTML = text 
    
    }, 2000);
    <div id="output">
        <h2 id="funny-day">Funny day: <span id="data" >(HERE IS WHERE I WANT THE OUTPUT TO BE)</span></h2>
    </div>

    I edited a few things that made no sense, for example your function cannot have - in it, Also, your markiser was not defined in the scope.

    Login or Signup to reply.
  2. Something like this?

    //Get elements
    var funnyday = document.getElementById("funny-day");
    
    //Array
    var list = ["Sunny", "Cloudy", "Rainy"];
    
    //Function to output array
    var index = 0;
    
    funnyday.innerHTML = ("Funny day: " + list[index]);
    index=index >= list.length - 1 ? 0 : index+1;
    setInterval(function() { 
      funnyday.innerHTML = ("Funny day: " + list[index]);
      index=index >= list.length - 1 ? 0 : index+1;
    }, 2000);
    <div id="output">
        <h2 id="funny-day">Funny day: </h2>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search