You have a string within an element, the element is clickable and can have an event on it. However, to determine the exact character you are clicking on you will need to wrap the character in an element that can be identified as its own selector.
One way to do this would be to get the textContent of the element (the characters), split the string creating an array of each character. Then iterate over that array and create a span
tag, add a class to it for click events, and place each characters value into each span
tag. Then append each tag back into the element.
Then you can create an eventListener that listens for clicks on the class that was added.
function getChar(e) {
console.log(e.target.textContent)
}
function createClickables() {
const str = document.querySelector('.info')
const parts = str.textContent.split('')
str.textContent = ''
parts.forEach(char => {
let span = document.createElement('SPAN')
span.textContent = char
span.classList.add('char')
str.append(span)
})
}
createClickables()
const chars = document.querySelectorAll('.char')
chars.forEach(item => {
item.addEventListener('click', getChar)
})
p {
font-size: 3rem;
}
.char {
cursor: pointer;
}
<p class="info">ABCDEFGHIJKLMNOPQRSTUVWXYZ</p>