Skip to content

Commit

Permalink
keep track of selected language
Browse files Browse the repository at this point in the history
  • Loading branch information
n-kall committed Sep 17, 2024
1 parent b012c7c commit a283e98
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions assets/script.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
document.addEventListener("DOMContentLoaded", function() {
const langSelector = document.getElementById("language-selector");

// Load the selected language from localStorage if available
const savedLang = localStorage.getItem("selectedLanguage");
if (savedLang) {
langSelector.value = savedLang;
}

// Function to show/hide code examples based on the selected language
function updateExamples() {
const selectedLang = langSelector.value;
document.querySelectorAll(".example").forEach(function(example) {
console.log(selectedLang);

// Save the selected language to localStorage
localStorage.setItem("selectedLanguage", selectedLang);

document.querySelectorAll(".example").forEach(function(example) {
if (example.classList.contains(selectedLang)) {
example.style.display = "block";
} else {
Expand All @@ -13,6 +23,9 @@ document.addEventListener("DOMContentLoaded", function() {
});
}

// Listen for changes in the dropdown menu
langSelector.addEventListener("change", updateExamples);
updateExamples(); // Initialize on load

// Initialize by showing the appropriate examples when the page loads
updateExamples();
});

0 comments on commit a283e98

Please sign in to comment.