I have a form on one navigation tab and I link to it from another navigation tab. I'm using the code below to link to all my navigation tabs from multiple pages.
const hash=window.location.hash; const bsTab = new bootstrap.Tab(hash); bsTab.show();
It works, but:
If possible I would like to remove the hashtag after redirecting from the url. This is a problem I'm having with this part of javascript.
Another one is: In order to use a button to link to my form, I came up with the following solution:
function redirectToForm() { window.location.href = "http://www.example.com/#nav- contact-me-tab"; localStorage.setItem("selectedOption", "2"); } const hash=window.location.hash; const bsTab = new bootstrap.Tab(hash); bsTab.show(); var selectedOption = localStorage.getItem("selectedOption"); document.getElementById("reason").value = selectedOption; localStorage.removeItem("selectedOption");
At first glance this works, however, if I end up linking back to the form, the default option is no longer selected. In some cases, no options are selected. I want the form to always show the default selected option (which is "0" by default, otherwise it will be disabled) unless my button is clicked. I want to change this value to option 2 only when my button is clicked. If I redirect to my form from another link, I want the default disabled value to be displayed
This is my button:
<button id="gotoformselect2" onclick="redirectToForm()">Request References</button>
Perhaps someone who knows more about javascript can:
Otherwise, if I link to a form or any other navigation tab, I just want to use this code:
const hash=window.location.hash; const bsTab = new bootstrap.Tab(hash); bsTab.show();
If you can help, please let me know! Thanks in advance.
P粉0072885932024-03-23 09:03:22
function redirectToForm() { window.location.href = "http://www.example.com/#nav-contact-me-tab"; localStorage.setItem("selectedOption", "2"); } if (location.hash) { const hash = location.hash; const bsTab = new bootstrap.Tab(hash); bsTab.show(); history.replaceState('', null, location.origin+location.pathname); //replaces the hash } if (localStorage.getItem('selectedOption')) { const option = localStorage.getItem('selectedOption'); document.getElementById('reason').selectedIndex = option; localStorage.removeItem("selectedOption"); }