I’m struggling a little bit.
I have multiple elements containing a time in seconds.
<p><time class="replace_time">3.232151</time></p>
<p><time class="replace_time">5.234151</time></p>
...
jQuery(".replace_time").text(function(){
var d = jQuery(this).text();
var h = Math.floor(d / 3600);
var m = Math.floor(d % 3600 / 60);
var s = Math.floor(d % 3600 % 60);
if (h > 0) {
return (("0" + h).slice(-2) + ":" + ("0" + m).slice(-2) + ":" + ("0" + s).slice(-2));
}
else {
return ("0" + m).slice(-2) + ":" + ("0" + s).slice(-2);
};
});
It’s basically working but unfortunately only for the last (??) item in the DOM order 🤔 What do I overlook?
2
Answers
I think you should use the .each function to get every element
@ErnestasL has your answer, you need to iterate over the array returned by the initial query.
If you split out the time formatting part, the rest is pretty simple without jQuery getting in the way: