I have the following jQuery/JS that triggers some stuffs when an element is clicked:
$(tabs).click(function(e) {
tabs.removeClass('active');
$(this).addClass('active');
$(tabIDs).removeClass('js_tab_area_show');
$($(this).attr('href')).addClass('js_tab_area_show');
e.preventDefault();
alert('worked');
});
tabs
here has been initialised before the function and has selected the elements I want to target. All the things above are already working. However, I now want to disable the anchor element (i.e. tabs
in this case) from scrolling to the element with the ID that is contained within the href
properties of my anchors. I tried looking around and many suggested preventDefault()
to prevent scrolling. However, it did not work for me. The only difference I mostly see is they have a named function dedicated for handling click events that is called from within the click function. Mine on the other hand did the above. The alert('worked')
did fire indicating the code executed until the end. What did I do wrong here?