Thanks to @redsquare's answer on question Javascript/jQuery - How do I obtain name of the class of clicked element? I am using the following JS to log innerText of the CSS class and a parent element.
The code has the desired outcome in console however I need to return the value instead of using console.log(). What would be the best way to do this?
I need to pass this value into a tag management system.
My attempts to include a return statement into the existing code have not worked.
window.onclick = function(e) {
var classList = e.target.className.split(/\s+/);
for (i = 0; i < classList.length; i++) {
if (classList[i] === 'myClass') {
console.log(e.target.innerText + " - " + e.target.parentNode.parentElement.children[1].innerText);
}
}
}
<span class="myClass">Click here</span> and more text