11

I want to make jQuery navigates directly (no animation need) to a id that I pass in a variable.

I have various marks like id="content", id="edit", id="..." that are <h2> titles. Doing validation with PHP I will output a variable like var NAVIGATE_TO = <?php echo $where_failed;?> and I want to move the website to that id position.

Like if I do domain.tld/page#edit or #content but with jQuery because when I load the page my PHP framework doesn't allow me to indicate the hash.

5 Answers 5

41

You can set location.hash to the id you need the browser to scroll to:

window.location.hash = '#edit';
3
  • Do you need the "#" in the hash string?
    – Chris
    Mar 4, 2011 at 11:43
  • 3
    @Chris It's optional I believe, but getting location.hash will return a string including the #
    – Yi Jiang
    Mar 4, 2011 at 11:45
  • ah, ok. I had assumed that the # would be considered a separator and thus not part of the hash itself. There's my little lesson for the day. :)
    – Chris
    Mar 4, 2011 at 12:01
21

In my experience the window.location.hash solution only works once. If you don't want to use the plugin you could try this:

var navigationFn = {
    goToSection: function(id) {
        $('html, body').animate({
            scrollTop: $(id).offset().top
        }, 0);
    }
}

and then call it like so (where someID is the ID of the element you wish to scroll to):

navigationFn.goToSection('#someID');

With this you can also vary the animation speed (I have it at 0) so that it is instant, but you could pass the value to the function so the code is reusable.

1
  • Pretty useful. Thanks!
    – Sergio
    Mar 11, 2019 at 2:46
5

Use the jquery scrollto plugin then you can do it like this

$(document).ready(function(){  
      $(".topMenu").click(function() {
        $.scrollTo($("#edit"), { duration: 0});
      });
4

I know it's an old question, but I thought it would be also a good solution if just want the element to be visible in the browsers view. (scroll without any animation).

var elem = document.getElementById("yourContent");
elem.scrollIntoView();

The above sample is taken from https://www.w3schools.com/jsref/met_element_scrollintoview.asp

1
  • 1
    This should be default answer. Just plain JS with no tricks, hashtags, etc.
    – arthas
    Aug 12, 2019 at 23:33
0

the question is for "jQuery"
with jQuery
@Patrick 's answer will be right and here is the example without function:

$('html, body').animate({scrollTop: $("#id").offset().top}, 0);

with javascript:
@Yi Jiang 's answer is javascript based

window.location.hash = '#idWithOrWithoutHashSign';

@Yusufbek answer works.
but why create variable?
instead:

document.getElementById("idWithoutHashSign").scrollIntoView();

also check difference with "smooth";

document.getElementById("idWithoutHashSign").scrollIntoView({behavior: "smooth"});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.