I've used jQuery to open and close some divs.
The code works fine until I try to use it as a function, where, I think due to context / or scoping issues the jQuery(this)
that I've been using as a selector doesn't work when executed from a function.
The function is this:
function open_close_sections() {
if(jQuery('.question').hasClass('open')) {
jQuery(this).removeClass('open');
jQuery(this).next().slideUp();
}
else
{
jQuery('.open').not(this).next().slideUp();
jQuery('.open').not(this).removeClass('open');
jQuery(this).addClass('open');
jQuery(this).next().slideDown();
}
}
and I'm trying to invoke it with
jQuery('.question').click(open_close_sections());
You can see the code working outside of a function at this pen: http://codepen.io/amort2000/pen/KVqaqr and failing to work in a function at this pen: http://codepen.io/amort2000/pen/YwQNOQ