Why won't jQuery toggle this div?

The first thing is that your code is malformed. For example.

The first thing is that your code is malformed. For example, Category 3 cat 3 story1 head ... the story... cat 3 story2 head ... another story... has one too many /div tags in it. It should look like this... Category 3 cat 3 story1 head ... the story... cat 3 story2 head ... another story... I did a little bit of reordering in the click function... $(".

Category-hed"). Click( function() { // TODO: this doesn't collapse an already-expanded section... why? $("h2", this).

ToggleClass("expanded"); // 2nd arg to $() sets context of the selection: h2 under this $(this). Siblings(". Stories").toggle(); // show this category's "stories" div $(".

Category-hed"). Not(this). Find("h2").

RemoveClass("expanded"); // unset the 'expanded' indicators in all categories $(". Category-hed"). Not(this).

Siblings(". Stories").hide(); // hide the "stories" divs in all categories }); Try that out and let me know how it goes!

Doh, the extra divs were a code-cleanup bug -- I should have seen that because the stories div is a sibling of the category-hed div, not a child. Fixed. Your reordered click function works great -- thanks!

– Val Dec 9 '10 at 19:14 glad to hear it! :-) – mikesir87 Dec 9 '10 at 21:04.

If I undersand your question correctly, it's because when you click on the already-expanded category-hed, it is first "collapsed" (by removing the expanded class), then the expanded class is immediately re-added by .toggleClass(). In your code, there is always going to be something that ends up being expanded, because prior state is not accounted for. Something as simple as this should fix the problem: $(".

Category-hed"). Click( function() { if($(this). HasClass("expanded") var dont_reexpand = true; $(".

Category-hed h2"). RemoveClass("expanded"); // unset the 'expanded' indicators in all categories $(". Stories").hide(); // hide the "stories" divs in all categories if(!dont_reexpand) { // TODO: this doesn't collapse an already-expanded section... why?

$("h2", this). ToggleClass("expanded"); // 2nd arg to $() sets context of the selection: h2 under this $(this). Siblings(".

Stories").toggle(); // show this category's "stories" div } }); There are probably better ways to do this, but you get the idea.

You're right about not accounting for prior state. But, your example didn't work for me, even after cleaning up some minor syntax probs (missing close paren on the first if statement; Mike's worked out of the box. Thanks, tho!

– Val Dec 9 '10 at 19:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions