UI Dev
UI Dev

Reputation: 699

Remove '#' from a URL in JQuery

I have written a script remove the # from URL & scroll to the particular div. Its working for the normal url like

10.0.1.22/dev/pg/blog/all/

But its not working for the URL like

10.0.1.22/dev/pg/search/?tag=a&entity_subtype=blog&entity_type=object&search_type=entities#39531

Its not removing the # from URL & also the scrolling also not working.

Script

    jQuery(document).ready(function($) {

    $(".full_view_btn").live('click', function(){
        var divid = $(this).attr('data-id');
        var long_desc = "#inner_descrip" + divid;   
        var short_desc = "#listing_desp" + divid;
        var shw_mre_btn = "#full_view" + divid;

        if($(long_desc).is( ':hidden' )){
            $('.long_desc').hide();
            $('.short_desc').show();
            $(long_desc).show();
            $(short_desc).hide();
            $('.full_view_btn').text('Show more');
            $(shw_mre_btn).text('Show less');
          } else{
              $(long_desc).hide();
              $(short_desc).show();
              $(shw_mre_btn).text('Show more');
          }

        if (location.pathname.replace('/^\//','') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
          var target = $(this.hash);
          target = target.length ? target : $('[id=' + this.hash.slice(1) +']');
          if (target.length) {
            $('html,body').animate({
              scrollTop: target.offset().top-60
            }, 500);
                target.parent().find('#highlight_div'+divid).css("box-shadow", "0 0 2px 1px #cccccc");
            setTimeout(function() {
                target.parent().find('#highlight_div'+divid).css("box-shadow", "none");
            }, 1000);
            return false;
          }
        }

    });

});

Any idea guys how to fix this ? I cant find where is the error ?

Upvotes: 3

Views: 449

Answers (1)

Martin Surynek
Martin Surynek

Reputation: 658

you cannot use jQuery object to wrap hash string otherwise you will get empty array because the input parameter for jQuery should be an selector. You need target as DOM element wrapped by jQuery object to use methods like offset.

So you have to change the target to or whatever should be the target to scroll down

var target = $(this.target);

Upvotes: 1

Related Questions