zach
zach

Reputation: 478

jquery expand collapsed when coming from another page

I do not think this is possible but just want to be sure. I am trying to expand a collapsed div (with jquery) when coming from another page. For example: from the home page a user clicks on view press release and it goes to the page with many press releases already collapsed. I want the correct press released expanded. Is this even possible?

Upvotes: 1

Views: 875

Answers (6)

faicom
faicom

Reputation: 11

  1. Shows the Collapse DIV and won't remove/hide it

     $(document).ready(function() {
       $(window.location.hash).show();
     }); 
    
  2. Shows the Collapse DIV and collapses it after clicking on another collapse button/link within same page

     $(document).ready(function() {
       $(window.location.hash).addClass('collapse in');
     }); 
    

Upvotes: 0

tucuxi
tucuxi

Reputation: 17955

It is definitely possible - assuming you control the target page. In that case, you can add an extra argument to your request link specifying the press release tho expand (for example, mypage.html?press_release_id=1234)

And then, in the target page (the one with the press releases), and using getParameterByName() from How can I get query string values in JavaScript?, you can easily parse this argument and expand the corresponding div:

$(document).ready(
   function() {
      var id = getParameterByName("press_release_id");
      $("#" + id).show();
   });

Upvotes: 1

David Wick
David Wick

Reputation: 7105

you could use querystring to specify which press release to show.

here's how to parse the querystring: How can I get query string values in JavaScript?

assuming you use the function from the aforementioned link it would be something like:

on home:

<a href="press.html?pr=monkeysattack">Monkeys attack small children.</a>

on press.html:

$(function() { $('.pr').not('# + getParameterByName('pr')).hide(); }

Upvotes: 0

ecchymose
ecchymose

Reputation: 673

var hash = window.location.hash;
$('div'+hash+':first').show();

... to be used with something like: page.html#press-release-4

and a DIV having press-release-4 as ID of course

Upvotes: 0

kinakuta
kinakuta

Reputation: 9037

What about looking at the document.referrer? If the url contains the information that would refer to the specific div, then just trigger the event that would expand it in your ready handler.

Upvotes: 0

Thomas
Thomas

Reputation: 1197

On the document ready function expand the ones you want.

$(document).ready(function(){
 //expand collapse here
});

Upvotes: 0

Related Questions