tags:

views:

43

answers:

4
<div class="tabs">
<ul>
  <li class="active"><span class="l"></span><a href="#">Standard Class</a><span class="r"> </span></li>
<li><span class="l"></span><a href="#">Bussiness Class</a><span class="r"></span></li>
<li><span class="l"></span><a href="#">Premium</a><span class="r"></span></li>

and how to I take off the class active off the non selected one and put it on the selected one with jQuery

+1  A: 
var activeText = $('.active a').text();
Darin Dimitrov
...and part 2 of the question?
T.J. Crowder
That part is not clear. What is the *non selected one* and the *selected one*?
Darin Dimitrov
+2  A: 
$('.tabs a').click(function() { 
 $('.tabs li').removeClass('active');
 (this).parent().addClass('active');
});

and for the text of the active:

$('.tabs .active a').text();

Litso
+1  A: 

Live example: http://jsbin.com/uwigo4/3 (full source below)

Getting the text of the "active" one, you can use text:

var text = $('div.tabs li.active').text();

Changing which is the active one, you just removeClass and addClass as appropriate, e.g.:

jQueryObjForCurrentActiveElement.removeClass('active');
jQueryObjForNewElementToMakeActive.addClass('active');

If you're doing this in an event handler, odds are you'll want hasClass as well.

Source of live example:

<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<meta charset=utf-8 />
<title>Test Page</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"&gt;&lt;/script&gt;
<![endif]-->
<script type='text/javascript'>

jQuery(function($) {

  $('div.tabs > ul > li').click(function(event) {
    var $this = $(this);
    if (!$this.hasClass('active')) {
      display("Activating '" + $this.text() + "'");
      $('div.tabs li.active').removeClass('active');
      $this.addClass('active');
    }
  });

  function display(msg) {
    $('<p>' + msg + '</p>').appendTo(document.body);
  }

});​

</script>
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
  .active {
    font-weight: bold;
    background-color: #eee;
  }
</style>
</head>
<body>
  <div class='tabs'>
    <ul>
      <li class='active'><span>Tab 1</span></li>
      <li><span>Tab 2</span></li>
      <li><span>Tab 3</span></li>
    </ul>
  </div>
</body>
</html>​
T.J. Crowder
+1  A: 

Try this:

It caches your <li> elements, so you don't need to keep selecting them from the DOM.

var $lis = $('div.tabs > ul > li').click(function() {
    var text = $(this).addClass('active').text();
    $lis.not(this).removeClass('active');
});
patrick dw
Which is, or isn't, a good thing, depending on whether the list of tabs is dynamic. :-)
T.J. Crowder
@T.J. - Very true. I'm playing the odds that the tabs aren't dynamic, but even if they are, the `$lis` jQuery object could be updated (or replaced) when needed. Either way, I'd cache them, but that's a personal preference. :o)
patrick dw