Skip to content

Instantly share code, notes, and snippets.

@BryanBarrera
Created October 8, 2015 14:12
Show Gist options
  • Save BryanBarrera/5a975de03ddc9deff183 to your computer and use it in GitHub Desktop.
Save BryanBarrera/5a975de03ddc9deff183 to your computer and use it in GitHub Desktop.
Show/Hide element based on URL Parameter
// Show #element1 by default
// Show #element1 & #element2 by a unique url string
// --
// ! #element2 is hiding by default with a class of `.hide`
// --
// if the url string is `?sample=one`
// show `#element1` widget and hide the `#peakday` widget
if (location.search == "?sample=one") {
$('#element1').show();
$('#element2').hide();
}
// if the url string is `?sample=two`
// hide `#element1`
// we are also removing the class of `.hide` from the dom off `#element1`
else if (location.search == "?sample=two") {
$('#element1').hide();
$('#element2').show().removeClass('hide');
}
@vilbht
Copy link

vilbht commented Aug 2, 2017

Hi Bryan,

Is there a vanilla JS way of doing this? Would make a kick ass JS library that uses a data- parameter that defines the same value as the URL

So if for elements like:

<div data-url=abc1>
<div data-url=def2>
<div data-url=ghi3>

and the URL was website.com?option=abc1&ghi3
would show the 2 divs instead of 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment