views:

151

answers:

3

Sorry if this was answered in a previous thread, i couldn't find one.

I have 4 sections: Section1, Section2, Section3, Section4

There are classes for each section, so I am hiding and showing based on the section. The problem is some classes need to be showin in Section1 and Section2.

<div class="section1 section2">
blah blah
</div>

$('a.homeLink').click(function(){
  $('.section1').show();
  $('.section2, .section3, .section4').hide();
  return false;
});

As you see in this case I have a div in two sections, but as i thought would happen it remains hidden due to hiding class section2

work arounds or solutions??

thanks

+1  A: 

Well, if section1 always takes precedence over section2, just change the order of the calls:

$('.section2, .section3, .section4').hide();
$('.section1').show();

If it's not that simple, you'll need to tell us more about what the requirements are.

Syntactic
I agree with Syntactic here. One could get more specific with what you wanted by hide/showing by id name instead of generically by class name.
jimyshock
section1 does not always take precedence. They are posts that need to be shown in the sections, but some will show up on two or more. So lets say i have 10 posts with classes section1 but 2 of those posts also need to be shown in section2 so giving those posts two classes but they are getting hidden due to hiding section1 when section2 is revealed.
Xtian
I used IDs but the client said it would be to difficult for the users to make a unique ID every time. Instead of just putting the appropriate class to the section
Xtian
Alternately, hide *all* sections and show only the ones you want to see at the time. That may be a simpler approach as you only have to manage when the div is hidden already. This could be suboptimal in some scenarios if there's a bunch of other things happening on the page though.
Jim Leonardo
A: 

I recommend rethinking your CSS schema....or re-ordering your JavaScript events. There is a natural cascade to both CSS and script events that needs to be thought through in your schema. The more you fight against this natural cascade, the more confusing your CSS and JavaScript will be.

Bradley Mountford
A: 

You could add a generic section class and then filter on the "instance class" (eg. section1):

<div class="section section1 section2">
blah blah
</div>

<div class="section section3 section4">
blah blah
</div>

$('a.homeLink').click(function(){
  var sections = $('.section');
  $('section1', sections).show();
  sections.not('.section1').hide();
  return false;
});
prodigitalson