---
title: "How can I make link of a tab using JavaScript?"  
description: "How can I make link of a tab using JavaScript?"  
author: "Anonymous User"  
published: 2013-08-19  
updated: 2013-08-19  
canonical: https://www.mindstick.com/forum/1403/how-can-i-make-link-of-a-tab-using-javascript  
category: "javascript"  
tags: ["javascript"]  
reading_time: 2 minutes  

---

# How can I make link of a tab using JavaScript?

Hi!\

I have a sample [page](https://www.mindstick.com/articles/13031/why-to-make-a-wikipedia-page) with four tabs (tabs.html). Tabs are displayed as follows:

```
<ul class="tabs">    <li><a
href="javascript:tabSwitch('tab1', 'content for first tab');"
id="tab1" class="active"> first tab'</a></li>    <li><a
href="javascript:tabSwitch('tab2', 'content for second tab');"
id="tab2"> second tab </a></li>    <li><a
href="javascript:tabSwitch('tab3', 'content for third tab');"
id="tab3"> third tab'</a></li>  <li><a
href="javascript:tabSwitch('tab4', 'content for fourth tab');"
id="tab3"> fourth tab</a></li></ul>
```

The [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) used to [switch](https://www.mindstick.com/blog/104495/switch-bringing-new-advancements-in-the-education-management-system) tabs is:

```
function tabSwitch(active, number, tab_prefix,
content_prefix) {    for (var i = 1; i
< number+1; i++) {         
document.getElementById(content_prefix+i).style.display = 'none';         
document.getElementById(tab_prefix+i).className = '';      }     
document.getElementById(content_prefix+active).style.display =
'block';     
document.getElementById(tab_prefix+active).className = 'selected';      }
```

[Now](https://yourviews.mindstick.com/view/81402/it-s-liberals-vs-progressives-in-us-politics-now), by [default](https://www.mindstick.com/interview/12771/what-is-the-importance-of-default-resources), when I [link](https://www.mindstick.com/articles/23633/link-builder) to tabs.html, the first tab is set to [active](https://answers.mindstick.com/qa/97113/why-does-an-active-ftp-not-work-with-network-firewalls). Is it possible to create a link from another site that [links](https://www.mindstick.com/blog/415/css3-links) to tabs.html and forces the [second](https://answers.mindstick.com/qa/41761/how-did-the-second-industrial-revolution-influence-women-s-roles-in-society) or [third](https://yourviews.mindstick.com/story/4957/the-significance-of-the-third-eye-in-hinduism-more-than-just-a-symbol) tab to be active?

Ok

## Replies

### Reply by shreesh chandra shukla

Hi!

You can try to get the tab you want to add from the hash like tabs.html#tab1. Then on a code block that run on the page onload do something like this:

```
var hash = window.location.hash;if (hash == "#tab1")    tabSwitch('tab_1', 'content_1');else if (hash == "#tab2")    tabSwitch('tab_2', 'content_2');else if (hash == "#tab3")    tabSwitch('tab_3', 'content_3');
```

\


---

Original Source: https://www.mindstick.com/forum/1403/how-can-i-make-link-of-a-tab-using-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
