What is causing the if statement (totalViews === 0) to not function properly?
The statement should be displaying a span tag with in the "div.viewed" class. The span's inner text should read "0 people have viewed your post" if the "totalViews" equals 0 (no span tags to begin with). However, a span tag is not being inputted at all in to the "div.viewed" class.
The remaining if statements seem to be functioning properly.
A sample of the current code:
function checkViewers() {
$('div.viewed').each(function() {
//Base Variables
var viewer = $('span.user', this);
var totalViews = viewer.length;
var shortenViews = viewer.length -1;
var viewerCount = $('span', this);
if (totalViews === 0) {
$('div.viewed', this).append('<span> 0 people have viewed your post.</span>');
}
if (totalViews == 1) {
$('<span> has viewed your post.</span>').insertAfter(viewer.last());
}
if (totalViews == 2) {
$('<span> and </span>').insertAfter(viewer.first());
$('<span> have viewed your post.</span>').insertAfter(viewer.last());
}
if (totalViews >= 3) {
$('<span> and </span>').insertAfter(viewer.first());
$('<span class="user count"></span>').insertAfter(viewerCount.eq(1));
$('.count', this).html(shortenViews + ' more people');
$('<span> have viewed your post.</span>').insertAfter(viewer.last());
viewer.slice(1).hide();
}
});
}
View the current and complete Plunker.
$('div.viewed').each(function() {...});inside which you expect$('div.viewed', this).append(...);to insert something and so it would if the DOM were so constructed but$(this).append(...);would seem more appropriate.