Create a dynamic link based on checkbox values
<p>What I'm trying to achieve is this:</p>

<ol>
<li>Default state of page = no checkboxes ticked, no link shown</li>
<li>User ticks one (or more) checkboxes</li>
<li>Link appears, dynamically generated from the checkbox values, in the following format: <a href="http://example.com/?subject=Products&amp;checked=Blue,Green,Purple" rel="nofollow noreferrer">http://example.com/?subject=Products&amp;checked=Blue,Green,Purple</a> (where the selected checkbox values are "Blue", "Green" and "Purple")</li>
</ol>

<p>Thus far, based on advice from another question (<a href="https://stackoverflow.com/questions/40007622/using-javascript-to-load-checkbox-values-into-a-string/40008127">Using JavaScript to load checkbox Values into a string</a>), I've been able to get the values in the proper format (as part of the required url) printed to console.log via a button:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>$("button").on("click", function(){
 var arr = []
 $(":checkbox").each(function(){
    if($(this).is(":checked")){
   arr.push($(this).val())
    }
 })
 var vals = arr.join(",")
 var str = "http://example.com/?subject=Products&amp;" + vals
 console.log(str) 
})</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"&gt;&lt;/script&gt;
&lt;input type="checkbox" id="selected" name="selected" value="Blue" class="products"&gt; Blue&lt;br&gt;
&lt;input type="checkbox" id="selected" name="selected" value="Green" class="products"&gt; Green&lt;br&gt;
&lt;input type="checkbox" id="selected" name="selected" value="Purple" class="products"&gt; Purple
&lt;br&gt;
&lt;button&gt;button&lt;/button&gt;</code></pre>
</div>
</div>
</p>

<p>However, they're not loading dynamically based on the selected checkboxes (it requires you to press the button in order to generate the url) and the link hasn't been printed to the page for use by visitors (it's stuck in console.log, visible but not usable).</p>

<p>I've been advised that <code>.change()</code> might be the way to go here, in terms of generating the link dynamically. Something like: <a href="https://stackoverflow.com/questions/7031226/jquery-checkbox-change-and-click-event/7031408#7031408">jQuery checkbox change and click event</a>.</p>

<p>How can I merge the two approaches to achieve the result I'm looking for?</p>