dimanche 30 juillet 2017

Update search query with Javascript

I currently have a Javascript function to update facets. See code:

function onFacetChangeApplied(){
    var url = window.location.href.split("?")[0];
    var search_query = getParameterByName('q');
    var url_with_search_query = url + '?q=' + search_query 
    $('input:checkbox.facet').each(function () {
        var sThisVal = (this.checked ? $(this).val() : null);
        var sThisName = (this.checked ? $(this).attr('name') : null);
        if(sThisVal !== null){
            url_with_search_query += '&'+encodeURIComponent(sThisName)+'='+encodeURIComponent(sThisVal);
    }
});
location.href = url_with_search_query;
return true;

Next I have some filters that sort products in A-Z and Price Low to High. How can I create an 'onchange' script to update the search query. At this moment, any new filters are overwriting to existing search query, I want it to add on to it.

HTML:

            <div class="tab-filter">
                                <select class="selectpicker" data-style="btn-select" data-width="auto" onchange="getQueryParams();">
                                <option>Sort by</option> 
                                <option value="?q=&amp;sort=0 ">Price: Low - High </option>
                                <option value="?q=&amp;sort=1 ">Price: High - Low </option>
                                <option value="?q=&amp;sort=2 ">Retailer: A-Z</option>
                                <option value="?q=&amp;sort=3 ">Retailer: Z-A</option>

                                </select>




Aucun commentaire:

Enregistrer un commentaire