1

I'm working on a dropdown with javascript. My HTML does not work and I can't find the problem... I think I've done right but maybe it's the link who does not work.

$(document).ready(function() {
  $("article.hey > *:not(header)").hide();
  $("article.hey header").on("click", function() {
    $(this).nextAll("*").slideToggle();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
  <article class="hey">
    <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse ac metus quis ante elementum tristique sit amet quis lorem. Fusce placerat dignissim nunc vitae consequat. In sagittis velit vitae erat faucibus, vitae condimentum risus luctus. Aliquam
      consequat est pellentesque viverra hendrerit. Duis tincidunt mi sem, et rhoncus ante consequat id. Sed ex est, pulvinar et mi eu, placerat tincidunt lacus. Praesent congue turpis mi, at vestibulum
    </p>
  </article>
</header>

Thanks!

5
  • 1
    where is the dropdown? Commented Oct 3, 2018 at 8:36
  • what doesn't work? What link may be not working? What exactly are you going to achieve (that for sure is not a dropdown) Commented Oct 3, 2018 at 8:37
  • IT's actually not a dropdown. I don't know how to say it. It's when you click on the header, the text should drop down.. Hope you understand! :) Commented Oct 3, 2018 at 8:37
  • ... and when you click again, it should close Commented Oct 3, 2018 at 8:39
  • header is parent of article.hey but you used $("article.hey header") Commented Oct 3, 2018 at 8:39

1 Answer 1

2

The structure of your HTML must be the header tag inside the article tag:

$(document).ready(function() {
  $("article.hey > *:not(header)").hide();
  $("article.hey header").on("click", function() {
    $(this).nextAll("*").slideToggle();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<article class="hey">
  <header>I'm the header 'CLICK ME'</header>
  <p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse ac metus quis ante elementum tristique sit amet quis lorem. Fusce placerat dignissim nunc vitae consequat. In sagittis velit vitae erat faucibus, vitae condimentum risus luctus. Aliquam
    consequat est pellentesque viverra hendrerit. Duis tincidunt mi sem, et rhoncus ante consequat id. Sed ex est, pulvinar et mi eu, placerat tincidunt lacus. Praesent congue turpis mi, at vestibulum
  </p>
</article>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! So the article will be the parent. Now i understand!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.