Hi im having problems with my html filter system.
This is the Error:
Uncaught TypeError: Cannot read property 'getElementsByClassName' of null at scripts.js:40
The system works but i want the error to be fixed
filterSelection("all")
function filterSelection(c) {
var x, i;
x = document.getElementsByClassName("filterDiv");
if (c == "all") c = "";
for (i = 0; i < x.length; i++) {
w3RemoveClass(x[i], "show");
if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "show");
}
}
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];
}
}
}
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(" ");
}
var btnContainer = document.getElementById("myBtnContainer");
var btns = btnContainer.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btnContainer[i].innerHTML = Quotes[x]
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}
body {
background-color: #383838;
}
.Navbar {
background-color: rgb(26, 25, 25);
overflow: hidden;
margin: -16px -8px;
}
.Navbar a {
float: left;
color: #f2f2f2;
text-align: center;
text-decoration: none;
padding: 18px 20px;
font-size: 15px;
font-family: Arial;
font-weight: bold;
line-height: 20px;
}
.Navbar a:hover {
background-color: #333;
color: #f2f2f2;
}
.container {
overflow: hidden;
font-family: Arial;
font-size: 15px;
font-weight: bold;
}
.filterDiv {
float: left;
background-color: rgb(26, 25, 25);
color: #ffffff;
width: 100px;
line-height: 100px;
text-align: center;
margin: 4px;
display: none;
/* Hidden by default */
}
/* The "show" class is added to the filtered elements */
.show {
display: hide;
}
/* Style the buttons */
.btn {
border: none;
outline: none;
padding: 12px 16px;
background-color: rgb(31, 31, 31);
cursor: pointer;
color: white;
font-family: Arial;
}
/* Add a light grey background on mouse-over */
.btn:hover {
background-color: rgb(46, 46, 46);
}
/* Add a dark background to the active button */
.btn.active {
background-color: #666;
color: rgb(31, 31, 31);
}
.navbar2 {
background-color: rgb(31, 31, 31);
overflow: hidden;
height: 40px;
margin: 16px -8px;
}
a {
text-decoration: none;
}
h1 {
font-family: Arial;
text-align: center;
color: white;
}
.uiltje {
margin: 20px -8px;
}
<div id="myBtnContainer">
<button class="btn" onclick="filterSelection('all')"> Show all</button>
<button class="btn" onclick="filterSelection('games')"> Games</button>
<button class="btn" onclick="filterSelection('ai')"> AI</button>
<button class="btn" onclick="filterSelection('tools')"> Tools</button>
</div>
<div class="container">
<div class="filterDiv tools">Slimleren</div>
<div class="filterDiv games">SampleGame</div>
<div class="filterDiv ai">SampleTool</div>
</div>