How to get page to scroll to a section when a button is clicked

How to get page to scroll to a section when a button is clicked

I'm trying to get my page to scroll to a specific point on my page if there is errors after submitting a form. My page scrolls down but then jumps up to a different section.
My code only works for chrome.
I got this code in my jquery
  1. if (location.hash !=="") {
          $("html, body").animate({ scrollTop: $('#' + window.location.hash).offset().top }, 1000);
        }

but then I get this error in my firebug


Error: Syntax error, unrecognized expression: ##contact

....value:null},fb.error=function(a){throw new Error("Syntax error, unrecognized ex...