cong-min / TagCloud

☁️ 3D TagCloud.js rotating with mouse
https://cong-min.github.io/TagCloud/examples
MIT License
356 stars 91 forks source link

Adding onclick Events #6

Closed DRavenNISV closed 4 years ago

DRavenNISV commented 4 years ago

Hi mcc108!

Thanks again for taking the time to look at my previous issue. I have a single other question for you specific to my usage of the TagClouds.

I'm having a little difficulty understanding the source code of TagCloud.js. I was wondering how (or if) general onclick Events can be added to the tags of the cloud. I noticed that there is a handler for mousemove event for the changes in directions. I also noticed that when hovering over a tag a highlight is given as well as a 'clicking option'. Is there perhaps an option that I could add an eventlistener specific for the onclick events?

Thank you in advance.

whelmin commented 4 years ago

I implemented it this way, use event delegation bind event listener to TagCloud instance root element

var rootEl = document.querySelector('.content');
rootEl.addEventListener('click', function clickEventHandler(e) {
    if (e.target.className === 'tagcloud--item') {
        console.log(e.target.innerText);
        // your code here
    }
});
DRavenNISV commented 4 years ago

This works perfectly! Thank you very much for your dedication.