Dynamic Accordion with jQuery

Dynamic Accordion with jQuery

Hi all - first post!

I have a basic accordion that will run in an HTA (local machine).
However - the problem I am facing is not just seen in HTA, but also in regular web pages.
Problem is any accordion level within a SPAN tag will not behave as needed (to hide when another heading is clicked).

I would like to remove a level in the accordion dynamically. The accordion is JS of course, but unfortunately, most of the code that will dictate if the level is removed is in VBS.

Here is the basic JS at the top of the webpage:
  1. <script type="text/javascript" src="jquery.js"></script> 
  2.  
  3. <script type="text/javascript"> 
  4. $(document).ready(function(){
  5. $(".accordion h3:first").addClass("active");
  6. $(".accordion p:not(:first)").hide();
  7.  
  8. $(".accordion h3").click(function(){
  9. $(this).next("p").slideToggle("slow")
  10. .siblings("p:visible").slideUp("slow");
  11. $(this).toggleClass("active");
  12. $(this).siblings("h3").removeClass("active");
  13. });
  14.  
  15. });
  16. </script> 

Here is the HTML that is causing the issue.
  1. <div class="accordion"> 
  2.       <h3>Question One Sample Text</h3> 
  3. <p>Test 1</p> 
  4.       <h3>This is Question Two</h3> 
  5. <p>Test 2</p> 
  6.       <span id="alterDynamically">
  7.             <h3>Problem Heading</h3> 
  8.             <p>This paragraph will NOT hide because it is nested in a SPAN.</p> 
  9.       </span>
  10.       
  11.       <h3>Sample Question Heading</h3> 
  12. <p>Num 3</p> 
  13. </div>

So on line 9 you see a SPAN.
My VBS will simply do something like
  1. alterDynamically.InnerHTML = ""
This does work - it removes that level in the accordion.
HOWEVER, before it is removed, or if it is not required to be removed then this level never closes.
Where I can click on any heading and all other visible levels close, this one never closes.
I believe the problem is:
  1. $(this).next("p").slideToggle("slow")
  2. .siblings("p:visible").slideUp("slow");
When nested within the SPAN, the .siblings constructor cannot see the P - it can only see the SPAN.
If I change the .siblings to SPAN:visible, then the entire SPAN disappears when sliding up (which is expected through the code I guess).


Can anyone figure out a way that I can keep the SPAN tags there and allow the P tag below it to behave in the same way as any other P tag outside of the SPAN?