Rate_Chin
Rate_Chin

Reputation: 23

Show/Hide a div conditionally

How to show this div in the body if the URL contains #people substring

The URL

www.example.com/user=?232343_ads234324#people

The div

<div id="div1" class="seperate"></div>

Upvotes: 2

Views: 1154

Answers (4)

Tymek
Tymek

Reputation: 3113

Assuming the element is hidden at the beginning:

if (window.location.hash === "#people") {
    $("#div1").show();
}

But if you don't know the state, according to documentation:

$("#div1").toggle(window.location.hash === "#people");

If you want also show and hide this div with other hashes like #allThePeople I recommend:

$("#div1").toggle(new RegExp("people", 'i').test(window.location.hash));
// element.visibility (if you can find 'People' in current page URL)

Upvotes: 0

Tushar
Tushar

Reputation: 87203

You can use toggle(), if the URL consists of #people, #div1 is show, hide otherwise.

$('#div1').toggle(window.location.href.indexOf('#people') > -1);

Upvotes: 1

Prasanth Jaya
Prasanth Jaya

Reputation: 4736

Try this

  var url = document.location.toString();

  if (url.match('#people')) {
  $('#div1').show();
  }
  else {
  $('#div1').hide();
  }

Upvotes: 2

vdwijngaert
vdwijngaert

Reputation: 1555

Something like this?

if(window.location.hash != "#people") {
    $("#div1").hide();
}

Upvotes: 0

Related Questions