-1

I have a form in Google Sheets, which passes URL parameters to a page, which then should show content based on the URL parameters.

So if a person checks "item 1" and "item 2" in the Google Sheet, the parameter gets passed on as follows:

https://my-url.com/?item1=value1&item2=value2

Now there's several div containers, which are set to display:none with css. When the URL parameter is met, the hidden div container should show up. So the html is:

<div class="hidden" id="value1">This content should show, when the URL parameter value 1 is passed</div>
<div class="hidden" id="value2">This content should show, when the URL parameter value 2 is passed</div>

I've found some code online, which does pretty much that, but it can only display one div at a time: https://jennamolby.com/how-to-display-dynamic-content-on-a-page-using-url-parameters/

My problem is, that for every passed parameter, a field has to show.

Can anyone help me with this? I'm really not an expert in js by any means.

  • Just duplicate the relevant parts then? It uses `var dynamicContent = getParameterByName('dc');` to assign the value of one specific parameter, to a specific variable - so duplicate that? And then duplicate the rest of the logic, that shows/hides one specific div, based on this one variable, as well … – CBroe Mar 18 '21 at 10:22

3 Answers3

0

You could do something like this:

$.each(getUrlVars(), function(i, x) {
  $('#' + x).show();
})

function getUrlVars() {
  var vars = {},
    hash;
  var hashes = url.slice(url.indexOf('?') + 1).split('&');
  for (var i = 0; i < hashes.length; i++) {
    hash = hashes[i].split('=');
    //vars.push(hash[0]);
    vars[hash[0]] = hash[1];
  }
  return vars;
}

getUrlVars is taken from this answer, and modified a bit.

Demo

var url = "https://my-url.com/?item1=value1&item2=value2"; //replace with window.location.href
$.each(getUrlVars(), function(i, x) {
  $('#' + x).show();
})



function getUrlVars() {
  var vars = {},
    hash;
  var hashes = url.slice(url.indexOf('?') + 1).split('&');
  for (var i = 0; i < hashes.length; i++) {
    hash = hashes[i].split('=');
    //vars.push(hash[0]);
    vars[hash[0]] = hash[1];
  }
  return vars;
}
.hidden {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="hidden" id="value1">This content should show, when the URL parameter value 1 is passed</div>
<div class="hidden" id="value2">This content should show, when the URL parameter value 2 is passed</div>
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
0

Good Luck

function getParameterByName(name, url = window.location.href) {
    name = name.replace(/[\[\]]/g, '\\$&');
    var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, ' '));
}

var item1 = getParameterByName('item1');
var item2 = getParameterByName('item2');

if (item1 == "value1")
{
  document.getElementById("value1").style.display = "block";
}
if (item2 == "value2")
{
  document.getElementById("value2").style.display = "block";
}
.hidden
{
  display: none;
}
<div class="hidden" id="value1">This content should show, when the URL parameter value 1 is passed</div>
<div class="hidden" id="value2">This content should show, when the URL parameter value 2 is passed</div>
John Doe
  • 1,401
  • 1
  • 3
  • 14
0

try this

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = window.location.search.substring(1),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return typeof sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
        }
    }
    return false;
};
let item1 = getUrlParameter('item1');
let item2 = getUrlParameter('item2');
if(item1 =='value1' &&item2 =='value2')
{
 document.getElementById('showhide').style.display = 'block';   
}
  
<div id="showhide" style="display: none">

hi hi
</div>
Mohammad Ali Rony
  • 4,695
  • 3
  • 19
  • 33