Skip to content

Instantly share code, notes, and snippets.

@vevgeniy85
Created March 21, 2023 10:59
Show Gist options
  • Save vevgeniy85/de9aabc17c5299cc30fe74df67bc486f to your computer and use it in GitHub Desktop.
Save vevgeniy85/de9aabc17c5299cc30fe74df67bc486f to your computer and use it in GitHub Desktop.
JavaScript - Set profile image as first letters of first and last name
function showAuthorInitials(containerSelector, blockSelector, attribute) {
const container = document.querySelector(containerSelector);
const blocks = container.querySelectorAll(blockSelector);
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i];
const value = block.getAttribute(attribute);
const words = value.split(" ");
let initials = "";
if (words.length > 1) {
words.forEach((word, index) => {
if (index < 2) {
initials += word.charAt(0);
}
});
} else {
initials = value.charAt(0);
}
block.innerHTML += `<div class="author-initials">${initials}</div>`;
}
}
// showAuthorInitials(containerSelector, blockSelector, attribute);
showAuthorInitials( ".featured-reviews-list", ".review-block__thumb", "data-author" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment