Get variable from query string, match it to an array in Javascript

I am trying to grab a query string parameter and match it to an array that is getting dynamically generated.

Edit for clarity The url will be something like www.example.com?some=stuff&id=dynamicNumber

I'm outputting my array from liquid (shopify) which seems to be rendering properly when I view the source.

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.

Fiddle with a static array and functional matching: http://jsfiddle.net/zHuDU/5/

I'm just missing the querystring function because I wasn't sure how to do that with JSfiddle

Here is my code:

<script>
  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('&');
    for (var i = 0; i < 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)");
  }
</script>

Answers


soldOut is string, but the values in your array are integer. Cast soldOut to an int:

soldOut = +GetQueryStringParams('id');

Need Your Help

getting facebook friends list who have installed an app

facebook facebook-graph-api facebook-fql friend

It's possible to know the facebook friends that they have installed a my fb app?

datatables with rowGrouping plugin aoColumns definitions fail

php jquery html css datatable

Ok so I am working on a table. And I am using rowGrouping on this particular table. To date I have had no serious problems with datatables until now.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.