views:

46

answers:

2

I am using a bit of JavaScript to show/hide sections of a site when a tab is clicked. I'm trying to figure out if there is a way I can link back to the page and have a certain tab open based on that link.

Here is the JS:

   var ids=new Array('section1','section2','section3','section4');

function switchid(id, el){  
    hideallids();
    showdiv(id);

    var li = el.parentNode.parentNode.childNodes[0];
    while (li) {
        if (!li.tagName || li.tagName.toLowerCase() != "li")
            li = li.nextSibling; // skip the text node
        if (li) {
          li.className = "";
          li = li.nextSibling;
        }
    }
    el.parentNode.className = "active";
}

function hideallids(){
    //loop through the array and hide each element by id
    for (var i=0;i<ids.length;i++){
        hidediv(ids[i]);
    }         
}

function hidediv(id) {
    //safe function to hide an element with a specified id
        document.getElementById(id).style.display = 'none';
}

function showdiv(id) {
    //safe function to show an element with a specified id        
        document.getElementById(id).style.display = 'block';
}

And the HTML

<ul>
<li class="active"><a onclick="switchid('section1', this);return false;">One</a></li>
<li><a onclick="switchid('section2', this);return false;">Two</a></li>
<li><a onclick="switchid('section3', this);return false;">Three</a></li>
<li><a onclick="switchid('section4', this);return false;">Four</a></li>
</ul>

<div id="section1"  style="display:block;">   
<div id="section2" style="display:none;">  
<div id="section3" style="display:none;">  
<div id="section4" style="display:none;">

I haven't been able to come up with a way to link back to a specific section. Is it even possible with this method?

Thanks!

A: 

HTML functionality is entirely independent of CSS. Therefore the following code will always work even if the intended section is set to display:none.

<a href="#section3">Link to section3</a>
I need this to work from an inbound link from another page. Something like <a href="pageName.aspx#section">. Unfortunately, this doesn't work. Neither does what you've recommended, actually. When I place your anchor on the page nothing happens.
`When I place your anchor on the page nothing happens.` Does the address in your browser change to the address of the current page + `#section3`?
Indeed. Try this:<a href="#section3">Link to section3</a><div id="section3" style="display:none">Hi</div>Does that work for you? I'm not sure how the anchor would override the display:none
Its not going to override the display:none. As I said in my post HTML functionality is entirely independent of CSS.
When you said the following code will always work even if the intended section is set to display:none, I guess I misinterpreted. I'm looking for something that actually opens the tab, set it's visibility to display:block. Perhaps I wasn't clear enough in my post. Thanks though.
+1  A: 

You could run some script when your page loads that checks the url hash & loads the appropriate section:

// on page load
var sectionid = /section\d/i.exec(location.hash);
if (sectionid) {
    var link = document.getElementById(switchid[0] +"_link");
    switchid(sectionid[0], link);
}

& add an id to your links:

<li><a id="section2_link" onclick="switchid('section2', this);return false;">Two</a></li>
jdeseno
Yes, this is what I'm looking for. Thank you. I'm having trouble getting it to work though. When I add this with an on page load event, nothing happens. Also, I can no longer click on the tabs to activate. I'm fairly new to javascript so I'm probably missing something. I'm a bit confused about reusing 'switchid' in the way you have.
There was an error with my usage of your function switchid. I cleaned my example up a little bit but, you'll certainly need to modify this to get it to work correctly. The broken tabs are probably a JS error, did you check your browser's javascript error console?
jdeseno