var test_words = [
{text: 'have', size: 102},
{text: 'Oliver', size: 47},
{text: 'say', size: 46},
{text: 'said', size: 36},
{text: 'bumble', size: 29, href: 'https://en.wikipedia.org/wiki/Beadle'},
{text: 'will', size: 29},
{text: 'Mrs', size: 56, href: 'https://en.wikipedia.org/wiki/Mrs.'},
{text: 'Mann', size: 27, href: 'http://educationcing.blogspot.nl/2012/06/oliver-twist-mrs-manns-character.html'},
{text: 'Mr', size: 27},
{text: 'very', size: 26},
{text: 'child', size: 20},
{text: 'all', size: 19},
{text: 'boy', size: 19},
{text: 'gentleman', size: 19, href: 'http://www.thefreelibrary.com/The+gentleman+in+the+white+waistcoat%3a+Dickens+and+metonymy.-a0154239625'},
{text: 'great', size: 19},
{text: 'take', size: 19},
{text: 'but', size: 18},
{text: 'beadle', size: 16},
{text: 'twist', size: 16},
{text: 'board', size: 15},
{text: 'more', size: 15},
{text: 'one', size: 15}
];
function deepCopy(oldValue) {
var newValue
strValue = JSON.stringify(oldValue)
return newValue = JSON.parse(strValue)
}
var cloud = d3.wordcloud()
.size([500, 275])
.transitionDuration(1000)
.fill(d3.scale.ordinal().range(["#884400", "#448800", "#888800", "#444400"]))
.words(deepCopy(test_words))
.onwordclick(function(d, i) {
if (d.href) { window.location = d.href; }
})
.start();
d3.select('#dataset-picker').selectAll('.dataset-button')
.on("click", function() {
//clear the wordcloud div
// cloud.remove();
document.getElementById("wordcloud").innerHTML = "";
var cloud = d3.wordcloud()
.size([500, 275])
.transitionDuration(1000)
.fill(d3.scale.ordinal().range(["#884400", "#448800", "#888800", "#444400"]))
.words(deepCopy(test_words))
.onwordclick(function(d, i) {
if (d.href) { window.location = d.href; }
})
.start();
});
<html>
<head>
<meta charset="UTF-8">
<title>Word Cloud</title>
<script src="https://cdn.rawgit.com/wvengen/d3-wordcloud/master/lib/d3/d3.js"></script>
<script src="https://cdn.rawgit.com/wvengen/d3-wordcloud/master/lib/d3/d3.layout.cloud.js"></script>
<script src="https://cdn.rawgit.com/wvengen/d3-wordcloud/master/d3.wordcloud.js"></script>
</head>
<body style="text-align: center">
<h1>Word Cloud</h1>
<div id='wordcloud'></div>
<div id="dataset-picker">
<input id ="test" value="test" class="dataset-button" type="button">
</div>
</body>
</html>