How to create a single-letter name avatar that works with Memberstack's dynamic loading? Answered
Hey guys, bit of a random one. Rather than users setting a profile picture, I just want to use a text element that takes the users first name and limits it to the first character only. I tried using some javascript to do this and it would work initially and then when the name loaded via memberstack it would break. Anyone have an idea on how to achieve this?
Here’s the code I’m using:
<script>
document.addEventListener("DOMContentLoaded", function () {
// Get the element containing the user's name
const userNameElement = document.getElementById("userInitial");
if (userNameElement && userNameElement.textContent) {
// Get the full name
const fullName = userNameElement.textContent.trim();
// Extract the first initial
const firstInitial = fullName.charAt(0).toUpperCase();
// Replace the content with the first initial
userNameElement.textContent = firstInitial;
}
});
</script>
It seems to briefly work on page load (https://coachesjunction.webflow.io/app/dashboard). However it then looks like something else is loading causing it to break.

Comments
6 comments
I've reviewed your code, and it worked perfectly for me without any issues. You may have to clear your cache and try again.
However, here’s an alternative approach that achieves the same result, which I believe will work seamlessly for you as well.
Hey Chukwudi, thanks for your reply. Unfortunately even after clearing the cache, it still looks like it’s not working. It’ll work initially and then as soon as memberstack loads (the test mode widget appears), it’ll then switch from the first initial to the full name.
Here’s a link to the loom. You’ll see it very briefly works on page load but then it looks like it is being overwritten or something when the page has finished loading.
I've just watched your video and that seems to be a cache issue. Here's a video of what it looks like on my end: Loom Link
Sorry Chukwudi, that’s because I manually set it to J and removed the data-attribute the other day to demo to the client and you just happened to sign up with a name that had J in it 😄 I’ve re-enabled it and cleared cache but still seeming to get the same issues
Ok. Let's go back to using the Memberstack code instead of localStorage.
Please use this instead:
Hey Jamie!
I saw your post and put together a solution for you! This simple script generates an avatar with a members initials when a member doesn’t have a profile picture. It works with Memberstack and Webflow. Let me know if you have any questions!
You can watch this tutorial and get the code at the link below🙏🏽
https://www.memberstack.com/scripts/initial-based-avatar
Please sign in to leave a comment.