I've got some code which has three page tabs, only one is shown at a time, with an active marker too however I cannot work out how I can link directly to the url
https://jsfiddle.net/c51xvekh/
My current html is:
function openTabs(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("city");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].classList.remove("active");
}
document.getElementById(tabName).style.display = "block";
document.getElementById(tabName + "-tab").classList.add("active");
}
document.getElementById("sunbed-tab").click();
<div class="breadcrumb-wrap">
<ul class="breadcrumb">
<li><a class="tablinks" id="sunbed-tab" onclick="openTabs(event, 'sunbed')">Sunbed<br>Packages</a></li>
<li><a class="tablinks" id="trainer-tab" onclick="openTabs(event, 'trainer')">Personal<br>Trainers</a></li>
<li><a class="tablinks" id="sauna-tab" onclick="openTabs(event, 'sauna')">Sauna<br>Rooms</a></li>
</ul>
</div>
<div id="sunbed" class="city">
This is sunbed page
</div>
<div id="trainer" class="city">
This is trainer page
</div>
<div id="sauna" class="city">
This is sauna page
</div>
for example - if I visit url.com/page#sauna then it opens the page with the sauna section opened and likewise for trainers if /page#trainer is visited.
Is there something in the JS that could do this?
I've just tried
if(window.location.hash.substring(1)=='trainer') {
document.getElementById("trainer-tab").click();
} elseif(window.location.hash.substring(1)=='sauna') {
document.getElementById("sauna-tab").click();
} else {
document.getElementById("sunbed-tab").click();
}
but it seems to fail with unexpected token.
(I'm a php dev and newly learning js)
I've tried running :target in the CSS and having it set to display: block but that didnt work.
Any ideas please?