javascript - Add the date on the top of each group of chats like in what's app -
i'm trying add date on top of messages sent , receive on particular day in chat app. instance:
-----yesterday----- mike: hello jan: hi -----today---- eunice: help! i've been trying day fid: me too. lets ask help.
i have written js calculate date , tried append top of messages sent daily. here i've done:
- add day of week classname messages sent on day
- use jquery select first element classname (day of week). firbase bomb yes sure i'm still learning though
- append html tag e.g
today
first div
here js
last10comments.on('child_added', function (snapshot) { var comment = snapshot.val(); var newdiv = $("<div/>").addclass("comment ").attr("id",snapshot.name()).appendto("#comments"); fb.api("/" + comment.userid, function(userdata) { comment.name = userdata.name; newdiv.html(mustache.to_html($('#template').html(), comment)); }); var outputtime = $("<span/>").addclass("timespan "+ te.getdayname() +"").attr("id",yiu).appendto("#comments").text(timeformat()); var tim = $('.'+ te.getdayname() +'')[0]; $('#'+tim+'').prepend( "<p>test</p>" ); })
this js gets first element. probelem is, prepend text based on number of element same class. i.e if 5 divs contain same classname, prepends 5 times. e.g wednesday wednesday wednesday wednesday wednesday
i have no way try out, guessing here, try this:
last10comments.on('child_added', function (snapshot) { (function (te) { var comment = snapshot.val(); var newdiv = $("<div/>").addclass("comment ").attr("id", snapshot.name()).appendto("#comments"); fb.api("/" + comment.userid, function (userdata) { comment.name = userdata.name; newdiv.html(mustache.to_html($('#template').html(), comment)); }); var outputtime = $("<span/>").addclass("timespan " + te.getdayname() + "").attr("id", yiu).appendto("#comments").text(timeformat()); var tim = $('.' + te.getdayname() + '')[0]; $('#' + tim + '').prepend("<p>test</p>"); })(te); })
let me know if works. if fixes problem, explain in comments.
Comments
Post a Comment