1

My whole JS file works except this piece of code:

    //Accordion
jQuery('#accordion').find('.accordion-toggle').click(function () {
    console.log(jQuery("#accordion").find(".accordion-toggle"))

    //Expand or collapse this panel
    jQuery(this).next().slideToggle('fast');

}); 

If I put this in the browser console it works fine.

user234562
  • 631
  • 9
  • 20
  • 2
    What error are you getting? – curv Dec 06 '16 at 15:42
  • It is even executing? Is there an error being thrown? Please be a little more articulate on what exactly is going wrong. – Tom Nijs Dec 06 '16 at 15:43
  • No error, I looks like it is not recognized – user234562 Dec 06 '16 at 15:43
  • Possible duplicate of [How can I debug my JavaScript code?](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) – Liam Dec 06 '16 at 15:44
  • From what you describe it sounds like either interfering code from something else on your page or the elements just don't exist yet. The DOM ready function will fix the latter issue so at least rule that out. – curv Dec 06 '16 at 15:45

1 Answers1

2

Try wrapping it in a DOM ready function:

$(function() {
    //Accordion
    jQuery('#accordion').find('.accordion-toggle').click(function () {
        console.log(jQuery("#accordion").find(".accordion-toggle"));

        //Expand or collapse this panel
        jQuery(this).next().slideToggle('fast');

    }); 
});
curv
  • 3,796
  • 4
  • 33
  • 48