Let me know if this works for you:
Option 1:
<script>
function openTable(evt, tableName) {
// select the target tab using its id
const table = document.querySelector(`#${tableName}`);
// scroll the target tab into view
table.scrollIntoView();
// and if you want to hide the other tabs
// select all the tabs
const allTabs = document.querySelectorAll('.tabcontent');
// filter all the tabs that we want to hide
const allOtherTabs = allTabs.filter(tab => {
return tab.id !== tableName;
});
// hide each tab
allOtherTabs.forEach(tab => tab.style.display = 'none');
// select all the tablinks aka Buttons
const allTabLinks = document.querySelectorAll('.tablinks');
// remove 'active' from their classList
allTabLinks.classList.remove('active');
evt.currentTarget.classList.add("active");
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
//////////////////////////////////////////
OR
Option 2:
If you want to use the anchor tag with the id you should replace:
this :
<button class="tablinks" onclick="openTable(event, 'NFL')">NFL</button>
with
this : `
<a href="#NFL" class="tablinks" onclick="openTable(event, 'NFL')">NFL</a>`
(i.e change all the buttons to anchor tags).
If you use the second option you don't have to use the .scrollIntoView()
method to get to the target.
There may be a typo or an error as i haven't tested it, try it and let me know if there is any problem i'd be happy to help. Good Luck!
//////////// Quick Fix /////////////
// first decrease the things you are loading in you're html by commenting them
HTML:
<div id="NBA" class="tabcontent">
<iframe src="https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=26&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1" width="100%" height="300" style="height:600px!important;border:0"></iframe><div style="padding:5px;text-align:center;font-size:10px">Powered by <a href="https://sport-tv-guide.live">Live Sports TV Guide</a></div>
</div>
<div id="MLB" class="tabcontent">
<iframe src="https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=42&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1" width="100%" height="300" style="height:600px!important;border:0"></iframe><div style="padding:5px;text-align:center;font-size:10px">Powered by <a href="https://sport-tv-guide.live">Live Sports TV Guide</a></div>
</div>
<div id="NHL" class="tabcontent">
<iframe src="https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=23&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1" width="100%" height="300" style="height:600px!important;border:0"></iframe><div style="padding:5px;text-align:center;font-size:10px">Powered by <a href="https://sport-tv-guide.live">Live Sports TV Guide</a></div>
</div>
<div id="NFL" class="tabcontent">
<iframe src="https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=40&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1" width="100%" height="300" style="height:600px!important;border:0"></iframe><div style="padding:5px;text-align:center;font-size:10px">Powered by <a href="https://sport-tv-guide.live">Live Sports TV Guide</a></div>
</div>
<div id="MLS" class="tabcontent">
<iframe src="https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=8&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1" width="100%" height="300" style="height:600px!important;border:0"></iframe><div style="padding:5px;text-align:center;font-size:10px">Powered by <a href="https://sport-tv-guide.live">Live Sports TV Guide</a></div>
</div>
// Comment out the divs bellow the above (MLS) div
// And then update you're script as bellow
Javascrip:
<script>
function openTable(evt, tableName) {
console.log(tableName);
// select the target tab using its id
const table = document.querySelector(`#${tableName}`);
// scroll the target tab into view
table.scrollIntoView();
// and if you want to hide the other tabs
// select all the tabs
const allTabs = Array.from(document.querySelectorAll('.tabcontent'));
// filter all the tabs that we want to hide
const allOtherTabs = allTabs.filter(tab => {
return tab.id !== tableName;
});
// hide each tab
allOtherTabs.forEach(tab => tab.style.display = 'none');
// select all the tablinks aka Buttons
const allTabLinks = document.querySelectorAll('.tablinks');
// remove 'active' from their classList
allTabLinks.forEach(tab => tab.classList.remove('active'));
// i forgot the line bellow in the option 1 script that was a bug
table.style.display = 'block';
evt.currentTarget.classList.add("active");
}
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
// this is not a permanent fix but it does work
// don't forget to comment the divs b/c every iframe tag is pulling some data and that's making you're browser unresponsive.
// there are better ways to do what you'r trying to do i'll hit you up with one real soon.
////////////// final answer ////////////
HTML: replace you're html with the code bellow
<div class="tab scroll-menue">
<button id = "NFL" class="tablinks" >NFL</button>
<button id = "MLB" class="tablinks" >MLB</button>
<button id = "NBA" class="tablinks" >NBA</button>
<button id = "NHL" class="tablinks" >NHL</button>
<button id = "MLS" class="tablinks" >MLS</button>
<button id="Liga-MX" class="tablinks" >Liga MX</button>
<button id="Premier-League" class="tablinks" >Premier League</button>
<button id="UEFA-Champions-League" class="tablinks" >UEFA Champions League</button>
<button id="LaLiga" class="tablinks" >LaLiga</button>
<button id="Bundesliga" class="tablinks" >Bundesliga</button>
<button id="Serie-A" class="tablinks" >Serie A</button>
<button id="Ligue-1" class="tablinks" >Ligue 1</button>
<button id="UEFA-Europa-League" class="tablinks" >UEFA Europa League</button>
<button id="UEFA-Europa-Conference-League" class="tablinks" >UEFA Europa Conference League</button>
</div>
JavaScript: replace you're script with the code bellow
<script>
const dataArr = [
{
id : "NFL",
src: "https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=40&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1",
},
{
id : "MLB",
src: "https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=42&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1",
},
{
id : "NBA",
src: "https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=26&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1",
},
{
id : "NHL",
src: "https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=23&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1",
},
{
id : "MLS",
src: "https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=8&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1",
},
{
id : "Liga-MX",
src:"https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=141&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1",
},
{
id : "Premier-League",
src: "https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=1&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1",
},
{
id : "UEFA-Champions-League",
src: "https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=19&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1",
},
{
id : "LaLiga",
src: "https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=2&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1",
},
{
id : "Bundesliga",
src: "https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=4&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1",
},
{
id : "Serie-A",
src: "https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=3&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1",
},
{
id : "Ligue-1",
src: "https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=6&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1",
},
{
id : "UEFA-Europa-League",
src: "https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=20&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1",
},
{
id : "UEFA-Europa-Conference-League",
src: "https://sport-tv-guide.live/widgetsingle/e7d7ce1978f9?list=4&id=2627&time_zone=US%2FEastern&&fc=1,7,44,102&time12=0&lng=1",
},
];
dataArr.forEach(obj => {
const html = `
<div id = '${obj.id}-content' class="tab-content-container">
<iframe src="${obj.src}" width="100%" height="300" style="height:600px!important;border:0"></iframe>
<div style="padding:5px;text-align:center;font-size:10px">Powered by <a href="https://sport-tv-guide.live">Live Sports TV Guide</a>
</div>
</div>`;
document.body.innerHTML += html;
});
const scrollMenue = document.querySelector('.scroll-menue');
const links = document.querySelectorAll('.tablinks');
const tabContentContainer = document.querySelectorAll('.tab-content-container');
links[0].classList.add('active');
const openTab = function(e){
const [dataObj] = dataArr.filter(obj => obj.id === e.target.id);
// if the click is not on any of the buttons exit
if(!dataObj) return;
document.getElementById(`${dataObj.id}-content`).scrollIntoView();
links.forEach(link => link.classList.remove('active'));
e.target.classList.add('active');
};
scrollMenue.addEventListener('click', openTab);
</script>
CSS: add the code bellow in you're style
div.tab button.active{
background-color: #c36d75;
}
I have tried first Option 2, since I am barely new with scripts. I have replaced the "button class" by "a href" two different times, one with the high comma and another one without the high comma at the end (think I understand what you did here but still opens the MLB competition when I go to #NFL link)
Do I need to do anything with the script you provided at option 1? Replaced my script with it but it doesn't display any content from the tables. Sorry but my knowledge at html and css is basic but at javascript is minimum – Alberto Aug 03 '22 at 00:18