Search code examples
javascriptjqueryhtmlwordpressrevolution-slider

Get hash from clicked item and scroll to


I need the hash from a clicked item. Using this hash you scroll to a specific anchor. The anchor is in a slider. I use the Revolution Slider. I dont know what is wrong in this code. It does not work. Does anyone have an idea and can help me? The code should not change much if possible, unless it is totally wrong. Any help will be appreciated. Thank You.

(function($){




$(document).ready(function() {

    $(".menu-item").click(function(e){
        e.preventDefault();

        var type = $(this).window.location.hash.substr(1);// get href from clicked item
        var hash ="#"+type;



            $('html, body').animate({
                scrollTop: $(hash).offset().top
            }, 2000, function () {
                revapi4.revshowslide($hash.data("slidenr"));
            });


    });

});
}(jQuery));

Solution

  • .menu-item has hash in href ?

    $(".menu-item").click(function(e){
      e.preventDefault();
    
      var href =$(this).attr('href');// get href from clicked item
      var hrefParts = href.split('#');
      var hash = '#'+hrefParts[1];
      alert(hash);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <a class="menu-item" href="/uri#hash-div">hash</a>