From a283e9821a68338edc8f8b7fed92e3535881e255 Mon Sep 17 00:00:00 2001 From: n-kall <33577035+n-kall@users.noreply.github.com> Date: Tue, 17 Sep 2024 14:10:35 +0300 Subject: [PATCH] keep track of selected language --- assets/script.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/assets/script.js b/assets/script.js index 29c05c8..e51d3df 100644 --- a/assets/script.js +++ b/assets/script.js @@ -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 { @@ -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(); });