Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Jquery Simple tabs

Posted on 2010-08-24
1
Medium Priority
?
455 Views
Last Modified: 2013-11-11
Hi,
I have to do a highly customized tabbed interface. I *dont* want to use the Jquery ui tabs - they're nice, but I need to do my own.
I am looking for ideas on how to implement this.

I am going to do something like:

<div class="tabs">
  <ul>
      <li><a class='tab1_link' href='#'>Tab 1</a></li>
      <li><a class='tab2_link' href='#'>Tab 2</a></li>
      <li><a class='tab3_link' href='#'>Tab 3</a></li>
  </ul>
  <div class='tab1' >content for tab 1</div>
  <div class='tab2' >content for tab 2</div>
  <div class='tab3' >content for tab 3</div>
</div>

Open in new window



$('.tab2').hide();
$('.tab3').hide();

$('.tab1_link').click(function(e){
  $('.tab1').show()
  $('.tab1').siblings().hide()
  e.preventDefault();
})

$('.tab2_link').click(function(e){
  $('.tab2').show()
  $('.tab2').siblings().hide()
  e.preventDefault();
})

$('.tab3_link').click(function(e){
  $('.tab3').show()
  $('.tab3').siblings().hide()
  e.preventDefault();
})

Open in new window


This is just my initial idea, the problem is that it doesnt scale - for every link I'd have to write a new function. The problem is that I don't know how to match a tabs link to a particular content div in a programmatic way.

Can anyone suggest a better way of achieving the above??

Cheers
John



0
Comment
Question by:jdav357
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 15

Accepted Solution

by:
SRigney earned 2000 total points
ID: 33511217
The way to match tab links to divs is to use the rel attribute of the anchor tag and the id of the div.



This should work as a rough version.   You'd probably want to add styling so it's clear which tab is selected and which ones are not.
<div class="tabs">
  <ul>
      <li><a class='tab_link' rel='tab1' href='#'>Tab 1</a></li>
      <li><a class='tab_link' rel='tab2' href='#'>Tab 2</a></li>
      <li><a class='tab_link' rel='tab3' href='#'>Tab 3</a></li>
  </ul>
  <div id='tab1' class='tab'>content for tab 1</div>
  <div id='tab2' class='tab'>content for tab 2</div>
  <div id='tab3' class='tab'>content for tab 3</div>
</div>

$('tab_link').click(function(e){
  $this = $(this);  // this is the link that was clicked.
  // hide all tabs.
  $(".tab").hide();
  $('#' + $this.attr('rel').val() ).show();  // show the clicked tab.
  e.preventDefault();
})

Open in new window

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question