I am using the following code to follow named anchors in jquery-ui-tabs. I have it so when the link is clicked it is supposed to take me to the appropriate tab and scroll to the id. The linking works (after much ado), however the scroll is only working if I put a breakpoint on the animate line.
jQuery(document).ready(function($) {
$('a[goto]').click(function(evt) {
evt.preventDefault();
var whereTo = $(this).attr('goto');
$tabs = $("ul.ui-tabs-nav li");
$tabs.find('a[href=#' + whereTo + ']').trigger('click')
$('html,body').animate({
scrollTop:$(this.hash).offset().top},
500);
});
});
});
I tried making it part of a callback, but still no luck.
$('a[goto]').click(function(evt) {
evt.preventDefault();
var whereTo = $(this).attr('goto');
$tabs = $("ul.ui-tabs-nav li");
$tabs.find('a[href=#' + whereTo + ']').trigger('click', function(){
$('html,body').animate({scrollTop:$(this.hash).offset().top}, 500);
});
});
What am I doing wrong?
triggermethod doesn’t accept a callback function, it triggers the event so that event handler is executed, you have the value usinggotoattributes, what you are trying to do finally gives you the value of this variable,actually that part is redundant.Simply usewhereTovariable.Note that
gotois not valid attribute, if you are using HTML5 consider usingdata-*attributes.