How to provide a click handler in JQCloud

I am using JQCloud to create tagcloud. It's nice and easy and fits my custom visual criteria. I would like the click handler to be called when the 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 the correct tooltip, but does not display an alert box on click. What am I doing wrong here?

thank

0


source to share


3 answers


Handlers in JQCloud must be specified as follows:

handlers : {click: function() { alert("it worked for" + x.NAME); }}

      



working example, http://jsfiddle.net/Q6348/7/

+2


source


A slightly different and perhaps easier way to implement this is to construct 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 node value 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!

0


source


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);
});

      

0


source







All Articles