I’m using JS in the Cornerstone Content JS section on an individual page, trying to get a snippet working.
I’ve tested the individual sections of code separately (the parts within if/else statements) and they seem to work just fine when executed by themselves, however when I put it all together with the if/else statements it seems to always evaluate to the user not being logged in.
I basically want to check if user is logged in, then display one message, and if user is not logged in, display another.
The page itself outputs a list of items. These are the 3 scenarios:
User logged in:
- If there are no items, I want to display a specific message to user.
- If more than one item, the page will simply display the listings to the user.
User not logged in: - Display a message directing them to register or login.
When the page runs now, it seems to always evaluate to the user not logged in, even though on inspection the body has the class ‘logged in’. Perhaps my code isn’t perfect, but I’m just wondering if there is something in the Content JS section I need to know about executing my code.
Is there something about how/when the elements and various classes are being added to the DOM I’m not taking into account? I know this is a bit outside scope of them, but any help is greatly appreciated!
(function($) {
if ($('body').hasClass('.logged-in')) {
var numAncillaries = $('#my-ancillaries-list-container .list-item').length;
if (numAncillaries === 0) {
$('#my-ancillaries-list-container').html("<p>No ancillary vendors have been added to your portfolio. If you haven't submitted any requests to vendors yet, visit the <a href='/find-ancillary-vendors'>Find Ancillaries</a> page to get started.");
}
} else {
$('#my-ancillaries-list-container').html("<p>You must be registered and logged in to add ancillaries to your portfolio. Registration is free and you can <a href='/register'>click here to register</a>. If you're already registered, <a href='/login'>click here to login.</a>");
}
})(jQuery);
. Well, maybe this will help someone else.