views:

50

answers:

4

So I'm trying to achieve something seemingly very basic in jQuery. I have a number of "headers," and when I click on a header I want the info beneath that header to slide down, and all other header info to slide up (so that only one header is showing info at a time).

Here's sudo code of what I want to happen:

$('.more-info-header:not(.jquery-processed)').addClass('jquery-processed').click(function(e) {
  $(this).children('.game-info-text').slideToggle("Slow");
  [all other ('.game-info-text')].slideUp(fast)
});

For the life of me though, I can't figure out how to code that second line ([all other ('.game-info-text')]). Any help is much appreciated!

(I'm implementing this as a drupal behavior, which is why I have the "jquery-processed" class being added)

+2  A: 

Something like:

$('.more-info-header:not(.jquery-processed)').addClass('jquery-processed').click(function(e) {
  $(this).children('.game-info-text').slideToggle("Slow");
  $('.more-info-header').not(this).children('.game-info-text').slideUp(fast);
  //                      ^-- removes the clicked element from all `.more-info-header`
});

Reference: .not()

Felix Kling
Thanks, works like a charm.
Jordan Magnuson
+1  A: 

Sounds like you're after the jQuery UI Accordion plugin.

Chris
Thanks for the reference Chris. I'll look into this... can the Accordion plugin be customized quite a bit, to work with image headers and whatnot?
Jordan Magnuson
All of the jQuery UI plugins are highly skinnable. If you check the top-right hand corner of the demo page, you'll see a drop-down menu for a "Theme". You can define your own theme for any jQuery plugin using plain ol' CSS. They even have a roll your own theme generator to make the initial tinkering a little easier: http://jqueryui.com/themeroller/
Chris
+1  A: 
$('.more-info-header:not(.jquery-processed)')
.addClass('jquery-processed').click( function(e) {
  var these = $(this).children('.game-info-text').slideToggle("slow");
  $('.game-info-text').not(these).slideUp("fast");
});

With the not() function you can exclude the already selected elements.

galambalazs
Thanks for the answer--I really appreciate it!
Jordan Magnuson
+1  A: 

Like this?

  $('.more-info-header').click(function(e){
    var target = e.target || e.srcElement;
   $(this).children().not(target).find('.game-info-text').slideUp('fast');
   $('.game-info-text', target).slideToggle("slow");
  });
Mic
Thanks for the answer Mic!
Jordan Magnuson