From 814d8f64a1b437307a6921d1b229339066c94645 Mon Sep 17 00:00:00 2001 From: camilo Date: Thu, 5 May 2022 16:04:40 +0200 Subject: [PATCH] JavaScript main file --- main.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 main.js diff --git a/main.js b/main.js new file mode 100644 index 0000000..1eae767 --- /dev/null +++ b/main.js @@ -0,0 +1,57 @@ +filterSelection("all") +function filterSelection(c) { + var x, i; + x = document.getElementsByClassName("word"); + if (c == "all") c = ""; + // Add the "show" class (display:block) to the filtered elements, and remove the "show" class from the elements that are not selected + for (i = 0; i < x.length; i++) { + w3RemoveClass(x[i], "show"); + if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "show"); + } +} + +// Show filtered elements +function w3AddClass(element, name) { + var i, arr1, arr2; + arr1 = element.className.split(" "); + arr2 = name.split(" "); + for (i = 0; i < arr2.length; i++) { + if (arr1.indexOf(arr2[i]) == -1) { + element.className += " " + arr2[i]; + } + } +} + +// Hide elements that are not selected +function w3RemoveClass(element, name) { + var i, arr1, arr2; + arr1 = element.className.split(" "); + arr2 = name.split(" "); + for (i = 0; i < arr2.length; i++) { + while (arr1.indexOf(arr2[i]) > -1) { + arr1.splice(arr1.indexOf(arr2[i]), 1); + } + } + element.className = arr1.join(" "); +} + +// Add active class to the current control button (highlight it) +var btnContainer = document.getElementById("legend"); +var btns = btnContainer.getElementsByClassName("btn"); +for (var i = 0; i < btns.length; i++) { + btns[i].addEventListener("click", function() { + var current = document.getElementsByClassName("active"); + current[0].className = current[0].className.replace(" active", ""); + this.className += " active"; + }); +} + + +/////////////. ADD SNIPPETS TO A DIV ..///////// + +var snippet = document.getElementsByTagName("strong"); +var sptContainer = document.getElementById("sptContainer"); + +for (var i = 0; i < snippet.length; i++) { + sptContainer.innerHTML += ""; +} \ No newline at end of file