 <p>Just dont rewrite lastQuote and whichQuoteToChoose on each click event. so i moved those variables out of click event :) </p> <pre><code> $(document).ready(function() { var lastQuote = ""; var whichQuoteToChoose = ""; $("#getQuote").on("click", function() { $.getJSON("http://codepen.io/thomasdean/pen/Yqmgyx.js", function(json) { var html = ""; //alert(lastQuote) while (whichQuoteToChoose === lastQuote) { whichQuoteToChoose = Math.floor(Math.random() * 12); // returns a number between 0 and 11 } lastQuote = whichQuoteToChoose; // this converts raw data into html json = json.filter(function(val) { return (val.id == whichQuoteToChoose); }); json.forEach(function(val) { html += "&lt;div class = 'quote'&gt;" html += "&lt;h2&gt;\"" + val.Quotation + "\"&lt;/h2&gt;&lt;h2&gt;" + val.Quotee + "&lt;/h2&gt;" html += "&lt;/div&gt;" }); $(".quote").html(html); }); }); }); </code></pre>