So to quickly summarize my problem:
For an assigment in my class I had to fetch json data and put it on screen in a table. This works fine and im not gonna include that part of the code cause I am 100% certain that this isn't the problem.
The hard part was to order them by gender by clicking three links at the top of the page. I had this working using three anonymous onclick functions but my teacher told me I need to try and not repeat multiple lines of code. So he showed me a way to put it in one function. This was at the end of class so I couldnt really do it straight away in my own file. I tried it at home but it doesn't seem to be working and I just can't figure out why because my three seperate onclicks still do the job.
these are the three seperate onclick functions that work. As you can see when u click the males link. It hides all the female rows. If you click the females it hides all the males and if u click all its shows everyone.
males.onclick = function() {
const rowsF = document.querySelectorAll("#female");
const rowsM = document.querySelectorAll('#male');
for (const row of rowsF) {
row.hidden = true;
}
for (const row of rowsM) {
row.hidden = false;
}
}
females.onclick = function() {
const rowsF = document.querySelectorAll("#female");
const rowsM = document.querySelectorAll('#male');
for (const row of rowsF) {
row.hidden = false;
}
for (const row of rowsM) {
row.hidden = true;
}
}
all.onclick = function() {
const rowsF = document.querySelectorAll("#female");
const rowsM = document.querySelectorAll('#male');
for (const row of rowsF) {
row.hidden = false;
}
for (const row of rowsM) {
row.hidden = false;
}
}
Now this is my adjusted code that looked cleaner and is shorter. But there must be a mistake here cause this doesn't seem to work and I just can't figure out why it doesn't. Any help?
males.onclick = gender("m");
females.onclick = gender("f");
all.onclick = gender("a");
function gender(stringGender) {
const females = document.querySelectorAll('#female');
const males = document.querySelectorAll("#male");
if (stringGender === 'm') {
hideList(females, true);
}
if (stringGender === "f") {
hideList(males, true);
}
if (sGeslacht === "a") {
hideList(females, false);
hideList(males, false);
}
};
function hideList(genderRow, isHidden) {
for (const row of genderRow) {
row.hidden = isHidden;
}
};
I am a beginner in javascript. I have been following a course the past three months and this is the last assignment we have before moving on to a new language. So if u see any other things in my code that I could do better feel free to tell me :)
Kind regards