Setting Value of a filter/dropdown based with Javascript

I need to set the value of this filter based on a querystring. I am using Sharepoint 2013 and this is a portion of code in a Content Editor webpart. I would like to pass the following variable ?type. (ie. Pages/News.aspx?type=[Variable]) The challenge is that the code is getting some options out of a SP list in order to populate the dropdown. Any assistance would be greatly appreciated.

<div class="news-filters">

<select class="document-sort" id="newsType" dcnIndex="0">
<option{{if _xData.NewsCategory==""}} selected="selected"{{/if}} value="all">All Categories</option>
{{:~choice.getSelectOptions(dcn[0], "NewsCategory", _xData["NewsCategory"])}}
</select>

</div>

June 17th, 2015 12:06pm

Hi,

If you want to set the value of filter based on a querystring, we can use the following jQuery to get the value from querystring, then set the value in dropdown list.

<script src="http://code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        //get type value from URL
        var type = GetQueryStringParams('type');

    });
    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];
            }
        }
    }
</script>

If I misunderstanding your requirement, please provide more information for further research.

Best Regards,

Dennis

Free Windows Admin Tool Kit Click here and download it now
June 18th, 2015 3:47am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics