I am creating a simple page with questions and answers.
This is how it looks:
SECTION > Question > Answer
And this keeps on repeating with different sections, Each section has multiple questions and each question has an answer. By default everything is set to display:none. Now when someone clicks on any of the section, the related set of questions should showup, and when someone clicks on the question, it;s answer should show up.
Only one section should be open at a time.
I kind of figured it would require parent(), child(), each() functions but I don’t know how and where to use them 🙁
I have previously used javascript and used to do it via ID basis, but since I’m using jQuery, I thought I’d rewrite the page using classes as .section, .question and .answer
Here is a the simple fiddle page where I have created li elements : http://jsfiddle.net/DKMJH/2/
Your HTML is invalid,
<li>elements don’t nest like that so the browser will probably try to fix it up and what sort of selector you want for the next question or answer depends on how the browser decides to mangle your HTML. You probably want this HTML:Now that we have valid HTML, we’ll know what the final structure really is and you can use
nextto find the appropriate single element to open:You also spelled
cursorwrong in your CSS but that won’t have any effect on your jQuery.Fix fiddle: http://jsfiddle.net/ambiguous/jcnMa/
ryudice was in first and got to the heart of the matter, I just needed more space than a comment to deal with the broken HTML issue. Using
nextonly works because the browser is restructuring your HTML into a single unordered list.If you only want one question open at a time, then close the other ones:
Demo: http://jsfiddle.net/ambiguous/jcnMa/1/