You can do it this way:
$("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 the hash in the URL or if you click on the menu, it will trigger the event click
where will open the Collapse.
Your code is working. It just has a few redundancies. You want to open Collapse when it comes from another page or when you click on the link on the same page?
– Sam
I still get it from JS :S.... It would have to be both... When the user comes from another page or when it is on the same page
– guilhermebellotti
It’s working in parts, because if I have content (another ID) inside Collapse, how do I redirect? In case he would have to open the Collapse and then take to the content
– guilhermebellotti