I have the following countdown script that outputs the number of days left until a specified date.
This works fine, but I would like to wrap each number retuned in a span so I can give each number a style as at the moment it is just outputting a number in a div.
#2: You’re trying to wrap each individual number in a span? Convert the number to a string. split the string; join the string, using "</span><span>" as the glue.
Output the result wrapped in a span: "<span>"+out+"</span>"
Note, the IF(—) logic is there to prevent 0s (“0 Days, 0 Hours”…etc). You can write the string template (a widely supported feature of ES6) as one line if you want the 0s to be displayed.
I had an old countdown timer and modified your code a little - the css isn’t the greatest, but it should get you working. I have to say you really don’t have to do much to the code as that is great. It’s basically just HTML/CSS work that I did.
The variables days, hours, minutes and seconds are all separate. They could be put into anything HTML tag, I just used the old HTML/CSS tags just as an example. I just used .textContent as I don’t like using .innerHTML.
The issue I have is actually separating the numbers in the days, so each single digit is in it’s own span or div. At the moment they are in a single div, e.g. <div class="day">90</div> but I am trying to have something like
Ah, I guess I didn’t fully understand the original request. If what you need is every INDIVIDUAL DIGIT wrapped in a tag I would first write a helper function:
function splitAndWrap(str,bef,aft){
str= str.toString().split('').join(aft+bef);
return str ? bef+str+aft:'' ;
}
Then, modify my earlier suggestion:
if (days) {
ouputHTML+=`<span class="days"> ${splitAndWrap(days,'<span>','</span>')} days</span>,`;
}
if (hours) {
ouputHTML+=`<span class="hours"> ${splitAndWrap(hours,'<span>','</span>')} hours</span>, and`;
}
if (minutes) {
ouputHTML+=`<span class="minutes"> ${splitAndWrap(minutes,'<span>','</span>')}minutes</span>, and`;
}
ouputHTML+=`<span class="seconds"> ${splitAndWrap(seconds,'<span>','</span>')} seconds</span>`;