views:

519

answers:

4

I'm struggling with this bit of code, and I'm not sure if it's even possible. I have a list of divs within a single parent element, and I need to collapse and expand certain sets. Here's an example:

<div id="parent">
  <div class="alway_show">ALWAYS SHOW ME</div>
  <div class="collapse_me">COLLAPSIBLE</div>
  <div class="collapse_me">COLLAPSIBLE</div>
  <div class="collapse_me">COLLAPSIBLE</div>
  <div class="alway_show">ALWAYS SHOW ME</div>
  <div class="collapse_me">COLLAPSIBLE</div>
  <div class="collapse_me">COLLAPSIBLE</div>
  <div class="collapse_me">COLLAPSIBLE</div>
</div>

So, in the initial state, .collapse_me will be display:none. There will be a link in always show to expand ONLY the collapsed divs below that particular .always_show div. I know this would be ten million times easier if the collapsible divs were in their own div, but I don't have control over the code. I have to make it work as is using jquery. Possible?

A: 
queen3
Thank you, I know it was dumb ;-) To look a bit smarter, I'd suggest use jQuery to make .collapse_me divs to be children of .always_visible. Then any jQuery-based menu might be applied, I guess. But CSS-only solution is much better, of course.
queen3
You know whats even dumber? Trying to look smarter by answering the question with an answer that isn't possible. Read the question, he can't change the HTML.
jfar
and its dynamic so an all css solution is useless!
geowa4
I don't try to look smarter, it's sort of an excuse. And I talk about changing DOM, not HTML. Anyway the solution is accepted already. But, once again, I just couldn't resist answering the "Possible?" question ;-)
queen3
A: 
var fncdiv = function(){
 var el = this;
 do{
  el = $(el).next();
  if ($(el).hasClass("collapse_me") )
   $(el).toggle();
  else
   break;

 }while (true) 
};

$('#parent div.alway_show').bind("click", fncdiv);
andres descalzo
A: 

You shouldn't need to use jQuery. It only requires some clever CSS:

#parent
{
    /* normal parent css here */
}

#parent div
{
  display: block;
}

#parent.collapsed
{
    display: block;
}

#parent.collapsed div
{
  display: none;
}

Selectors are applied in order of specificity. Since '#parent.collapsed div' is more specific than '#parent div', it should override. Now, all you need to do, is set the parent div's class, and you're done. You can use javascript to add/remove the 'collapsed' class to the DIV at runtime to toggle expansion without any additional effort:

// Mootools:
$('parent').addEvent('click', function()
{
  $('parent').toggleClass('collapsed')
});
jrista
i think he wants it dynamically collapsible/expandable, not static with css. especially since it will initially be hidden and a link will make them show.
geowa4
I understand that. I simply provided the CSS half of the problem. Using any kind of javascript, be it native, jquery, mootools, or something else, he can still dynamically add and remove the collapsed class to the div. By using the above CSS, it simplified the problem GREATLY.
jrista
I think geowa4's misunderstanding of how CSS can be used to simplify this problem is one of the REAL problems that jQuery introduces. People look to a jQuery-only solution to problems that can be much more efficiently solved using CSS for what it is, and only using script when CSS fails. Don't be so quick to dismiss CSS...it is far more efficient at handling things than jQuery. Use CSS as much as possible until it is incapable, and only then fill in the GAPS with jquery/script.
jrista
+3  A: 
$('div.always_show').nextAll().each(function() {
    if($(this).is('.collapse_me')) {
        $(this).toggle();
    }
    else {
        //this will halt the each "loop", stopping before the next .always_show
        return false; 
    }
});

Of course you should not use my initial selector 'div.always_show', but rather supply it the actual element, which will be the parent of the clicked link. For example:

$('#expand_anchor').parent().parent().nextAll()...
geowa4
That did it. Thank you!
Bjork24
np
geowa4