Open collapse via URL Bootstrap

1

How can I open a collapse via URL?

For example: clicking 'Link 1' it goes to page 'page1' and opens the user to collapse ' link1' , in this case the collapse is with ID link1

>

Just to get an idea of the environment, I put together a basic structure in JSFiddle. link

    
asked by anonymous 03.07.2018 / 17:45

1 answer

1

You can do this:

$("a.nav-link").click(function(e,i){
   if(i){
      var hash = i.ancora ? i.hash : i;
   }else{
      var hash = this.href.match(/#.+/)[0];
   }

   $(hash + '.collapse').collapse('show');

   if(i) hash = i.ancora || hash;

   $('html, body').animate({ scrollTop: $(hash).offset().top - 200 }, 500);
});

$(document).ready(function(){
   var hash = location.hash;
   var params = hash;
   if(!$(hash).hasClass('collapse')){
      hash = "#"+$(hash).closest(".collapse").attr("id");
      params = {hash: "#"+$(hash).closest(".collapse").attr("id"), ancora: params};
   }
   if(hash) $("a.nav-link[href$='"+hash+"']").trigger("click", params);
});

If there is a hash in the URL or if you click on the menu, it will trigger the click event where you will open the collapse.

    
03.07.2018 / 22:17