2

I have the following code, when the user click on toggle to show content, I want to add a hash and id to the url (http://www.somedomain.com/test.html#2111) . When closed I want to remove the #2111.

How do i do that using the toggle?

CSS

.toggle{
    display:inline-block;
    height:48px;
    width:48px;
    background:url("http://icons.iconarchive.com/icons/pixelmixer/basic/48/plus-icon.png");
}
.toggle:after{
    content:"View All";
    display:block;
    height:48px;
    line-height:48px;
    width:288px;
    margin-left:48px;
}
.toggle.expanded:after{
     content:"Close All";
}
.toggle.expanded {
    background:url("http://cdn2.iconfinder.com/data/icons/onebit/PNG/onebit_32.png");
}

JQUERY

$(document).ready(function () {
            var $content = $("div.moreinfo").hide();

            $(".toggle").on("click", function (e) {
                $(this).toggleClass("expanded");
                if($(this).hasClass('expanded')) {
                    $("div.plus").hide().removeClass("closed");
                    $("div.minus").show().addClass("opened");   

                } else {

                    $("div.plus").show().addClass("closed");    
                    $("div.minus").hide().removeClass("opened");


                }
                $content.slideToggle();

            });
        });

1 Answer 1

3

You can just set location.hash equal to an empty string:

location.hash = '';

or set it to a value:

$(document).ready(function () {
    var $content = $("div.moreinfo").hide();
    $(".toggle").on("click", function (e) {
        $(this).toggleClass("expanded");
        if($(this).hasClass('expanded')) {
           location.hash = someValue;
        } else {
           location.hash = '';
        }
        $content.slideToggle();

    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

thanks I had updated with ur code , but only problem is that it doesnt remove the #. after adding #somevalue. because of the hash isnt removed from url.. it jumps around the page..Is there a way to remove the hash so when i click on togle close open it own jump up..
No problem! There is no way to remove the the hash from the url without reloading the page. Perhaps you could give the element you want the user to be scrolled to an id and instead of setting to '' you could set it to that id targetElement

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.