I am using JQCloud for building a tagcloud. It's nice and easy and meets my user's visual criteria. I would like to have a click handler invoked when user clicks on a word:
var tag_list = new Array();
for ( var i = 0; i < stuff.length; ++i ) {
var x = stuff[i];
tag_list.push({
text: x.NAME,
weight: x.COUNT,
//link: this.mkUrl(x),
click: function() { alert("it worked for " + x.NAME); },
html: {title: this.mkTooltip(x)}
});
}
$("#"+containerdivname).append( $("<div></div>", {id:"wordcloud"}));
$("#"+containerdivname).children("#wordcloud").jQCloud( tag_list );
The word cloud renders fine, has proper tooltip, but does not show an alert box on click. What am I doing wrong here?
Thanks
Handlers in JQCloud is supposed to be specified like this:
handlers : {click: function() { alert("it worked for" + x.NAME); }}
working example, http://jsfiddle.net/Q6348/7/
A little different and maybe a easier way to implement it would be, to build your array like this:
var stuff = [];
stuff.push({"text":"n1","weight":23,"handlers" : {click: function(res) { alert("it worked for"+res.target.textContent);}}})
This will give you the value of the node you clicked on.
Or if you want to pass it to a function, you can use it like this:
stuff.push({"text":"n1","weight":23,"handlers" : {click: function(res) { run(res) }}})
function run(res){
alert("it worked for"+res.target.textContent);
}
Hope this solution helps!
you can always add a standard jquery handler for your click:
$(document).on('click', '#container-id .jqcloud-word', function() {
var word_value = $(this).val();
console.log(word_value);
});
来源:https://stackoverflow.com/questions/17037892/how-to-provide-a-click-handler-in-jqcloud