JSFiddle - React, Tailwind, and code Playground

by marcgrabow

HTML

<button class="headerbuttongreen" onclick="javascript: updateURLParameter(window.location.href, 'view-all', 'new-view-all-value');">Blah Blah Blah</button><br><br>

If the URL is http://www.domain.com/index.php?action=my_action then the default "view-all" value would be "Yes" so the  URL they would be directed to when they click the button is http://www.domain.com/index.php?action=my_action&view-all=Yes.<br><br>

If the URL is http://www.domain.com/index.php?action=my_action&view-all=Yes then when they click the button it would change to http://www.domain.com/index.php?action=my_action&view-all=No

JavaScript

function updateURLParameter(url, param, paramVal) {
    var newAdditionalURL = "";
    var tempArray = url.split("?");
    var baseURL = tempArray[0];
    var additionalURL = tempArray[1];
    var temp = "";
    if (additionalURL) {
        tempArray = additionalURL.split("&");
        for (i = 0; i < tempArray.length; i++) {
            if (tempArray[i].split('=')[0] != param) {
                newAdditionalURL += temp + tempArray[i];
                temp = "&";
            }
        }
    }

    var rows_txt = temp + "" + param + "=" + paramVal;
    return baseURL + "?" + newAdditionalURL + rows_txt;
}