0

Hi i created accordion function using jquery by toggleClass function.

my fiddle for this case

here while clicking accordion class a active class will added to the same class i added some styles to active class for displaying child classes.Now the problem is the accordion toggled if we click anywhere of accordion class or its child classes whats wrong on it? my JS

$('.accordion').on('click', function() {
            $(this).toggleClass('active'); 
        });
1
  • There's nothing wrong with it, that's how jQuery (and Javascript) works. If you don't want it to happen on .accordion, be more specific with your selector Commented Sep 24, 2013 at 11:48

3 Answers 3

3
$('.accordion > a.title').on('click', function() {
  $(this).parent().toggleClass('active'); 
});

As RGraham says, there was nothing wrong with it, it was doing exactly what you told it to. If you want it to toggle only when you click on the title, then you need to catch the events on the title, not the whole accordion div.

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

Comments

1

you have to use .parent() of Jquery function.

$('.title').on('click', function() {
    $(this).parent().toggleClass('active'); 
});

Live Demo

Comments

0

Try this:

   $('.accordion > a.title').on('click', function() {
       $(this).parent().toggleClass('active'); 
   });

or

   $('a.title').on('click', function() {
       $(this).parent().toggleClass('active'); 
   });

here is updated Demo

Comments

Your Answer

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