Search code examples
jqueryanchorjquery-eventsanchor-scroll

Anchor tag href points to div after clicking it adds undefined in url


I have code like this:

<a href="#div-part" title="title">go to div part<a>
<form>
...
</form>
<div id="div-part">
<div>

After clicking the anchor tag scroll automatically moves to div part but instead of #div-part in url it adds #undefined in URL. e.g. localhost:80/index.jsp#undefined.

Please help me on this.


Solution

  • Here is a solution to scroll to particular div

    $(function() {
        $('a').bind('click',function(event){
            var $anchor = $(this);
            $('html, body').stop().animate({
                scrollTop: $($anchor.attr('href')).offset().top
            }, 1000);       
    
        });
     });
    

    Check DEMO here.