trouble adding class to the closest div
I am trying to write a function that when an anchor link with a particular naming convention is clicked on in a page a css class will be added to the parent div. I'm not having any luck after hours of trying. On my test page I have two accordions. The link in the first one will jump to the text in the second one after it opens its accordion. I don't get any errors. But my second accordion is not open and I don't see that class "in" is added to the parent div. It's probably something obvious, but I'm a rookie at this.
My javascript is:
$('a').on('click', function() {
var href= $(this).attr('href');
if(href.substring(0,8) === "#section")
{
var anchor = href.slice(1);
alert('href is ' + href);//this works
alert('anchor is ' + anchor); //this works
$('#accordion .in').removeClass('in');
$(this).closest('.collapse').addClass('in');
//$('html,body').animate({scrollTop: $(anchor).offset().top - 65},1000);
}
});