Created
March 21, 2023 10:59
-
-
Save vevgeniy85/de9aabc17c5299cc30fe74df67bc486f to your computer and use it in GitHub Desktop.
JavaScript - Set profile image as first letters of first and last name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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