jQuery: How to replace specific divs content with a countdown
I made a script that takes date (text) from between a div: <div
class="countdown"> DATE HERE </div> and makes a countdown from it that
refreshes every 1 second.
My question is: If I have multiple divs <div class="countdown"> DATE HERE
</div>, how can I make the script to create countdowns from this divs
automatically. My current script only works if it's only one div like this
in page.
This is my script:
var targetDate = $(".countdown").text();
targetDate = new Date(targetDate).getTime();
// porneste counter-ul
setInterval(function() {
var currentDate = new Date().getTime();
var secondsLeft = (targetDate - currentDate) / 1000;
var days = parseInt(secondsLeft / 86400);
secondsLeft = secondsLeft % 86400;
var hours = parseInt(secondsLeft / 3600);
secondsLeft = secondsLeft % 3600;
var minutes = parseInt(secondsLeft / 60);
seconds = parseInt(secondsLeft % 60);
// afiseaza data rezultata
$(".countdown").html('<table cellspacing="0" border="0"
cellpadding="0" width="328" align="center"><tr> <td
width="82">'+days+'<span class="gray_text">d</span></td> <td
width="82">'+hours+'<span class="gray_text">h</span></td> <td
width="82">'+minutes+'<span class="gray_text">m</span></td> <td
width="82">'+seconds+'<span class="gray_text">s</span></td>
</tr></table>');
}, 1000);
No comments:
Post a Comment