Note that there are some explanatory texts on larger screens.

plurals
  1. POGet variable from query string, match it to an array in Javascript
    text
    copied!<p>I am trying to grab a query string parameter and match it to an array that is getting dynamically generated. </p> <p><em>Edit for clarity</em> The url will be something like www.example.com?some=stuff&amp;id=dynamicNumber</p> <p>I'm outputting my array from liquid (shopify) which seems to be rendering properly when I view the source.</p> <p>My failing point seems to be setting the actual variable "soldOut", because if I put one of the actual values that I know will be in the array, it returns true. </p> <p>Fiddle with a static array and functional matching: <a href="http://jsfiddle.net/zHuDU/5/" rel="nofollow">http://jsfiddle.net/zHuDU/5/</a></p> <p>I'm just missing the querystring function because I wasn't sure how to do that with JSfiddle</p> <p>Here is my code:</p> <pre><code>&lt;script&gt; var testArray = [ // shopify liquid to generate values {% for variant in product.variants %} {% if variant.inventory_quantity == 0 %} {{ variant.id }}, {% else %} {% endif %} {% endfor %} ]; function GetQueryStringParams(sParam) { var sPageURL = window.location.search.substring(1); var sURLVariables = sPageURL.split('&amp;'); for (var i = 0; i &lt; sURLVariables.length; i++) { var sParameterName = sURLVariables[i].split('='); if (sParameterName[0] == sParam) { return sParameterName[1]; } } } var soldOut = GetQueryStringParams('id'); if(jQuery.inArray(soldOut, testArray)!==-1) { console.log("The ID was in the test array (out of stock)"); } else{ console.log("The ID wasn't in the test array (so it's in stock or I messed up)"); } &lt;/script&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload