javascript - jQuery function only being run once -
using moments.js library i'm attempting grab value of datetime attribute every time tag , output time using natural language.
given this:
<time datetime="2014-06-27"></time>
the results be:
<time datetime="2014-06-11">17 hours ago</time>
using this:
$(document).ready(function(){ var time = $('time'), date = moment(time.attr('datetime')), update = function(){ time.html(date.fromnow()); }; update(); });
it works, first time element. additional time elements saying "17 hours ago".
it's guess function being run once on first instance of time.
how can make sure it's being run each, , doing efficiently?
you need loop through time tags. right applying first instance of <time>
. use jquery .each()
.
$(document).ready(function(){ var time = $('time'); time.each(function() { date = moment($(this).attr('datetime')); $(this).html(date.fromnow()); }); });
Comments
Post a Comment