user1968430
user1968430

Reputation: 1

Link to page with jQuery link selected?

What I have so-far:

$("#navigation li a[id]").click(function() {
  $("#" + this.id.substr(1)).show(600)
     .siblings('.toggleDiv').slideUp(600);
});




          <!-- Begin Naviagtion -->
     <div id="navigation">

             <ul>
                <li><a id="babout">Canada</a></li>
                <li><a id="bcontact">USA</a></li>
                <li><a id="bsearch">Mexico</a></li>
         </ul>   

         </div>
         <!-- End Naviagtion -->



<br /><br />





<!-- Begin Search -->
        <div class="toggleDiv" id="search" style="">
               Mexico Staff
        </div>
     <!-- End Search -->

 <!-- Begin About -->
        <div class="toggleDiv" id="about" style="">
                Canada Staff
        </div>
     <!-- End About -->

         <!-- Begin Contact -->
        <div class="toggleDiv" id="contact" style="">
                USA Staff
        </div>
     <!-- End Contact -->

http://jsfiddle.net/q6ncx/12/

I need to be able to link people to this page, with one of the three options already selected.

ie. mysite.com/staff.php#Canada - the "Canada" link would already be selected.

Is this possible with jQuery? I know this would be easily doable in php, but I'd rather stick with js if possible.

Upvotes: 0

Views: 93

Answers (2)

Alain1405
Alain1405

Reputation: 2955

Use internal links. Your url will be mysite.com/staff.php#Canada and your ancor will be

<a  href="#Canada" id="babout">Canada</a>

If you want to trigger the event with JS:

if(window.location.hash === "Canada" ||
window.location.hash === "somethingElse") {
    //do something
}

Upvotes: 0

charlietfl
charlietfl

Reputation: 171669

Get hash from url and trigger a click on appropriate link:

var hash=location.hash;

if( hash && hash.substr(1).length){
   /* use "filter" to match hash to link text*/
   $('#navigation li a').filter(function(){
         return $.trim( $(this).text())==hash.substr(1) ;
    }).click(); 
}

Upvotes: 1

Related Questions