Getting value for attribute

I have this custom javascript that is supposed to return the text from an attribute when a button is clicked. I can have it to output the text to console but my javascript-variable just returns “”. Any suggestions?

function() {
let textValue = null;
document.querySelectorAll(‘.course-table’).forEach((e) => {
const wrapper = e;
const buttons = wrapper.querySelectorAll(‘.btn’);
buttons.forEach((b) => {
b.addEventListener(‘click’, () => {
const text = b.title || b.textContent;
textValue = text;
}, false);
});
});
return textValue;
}