1

so I am trying to make a page on my blog that has a list of the games that I have posts about and I am trying to make a filter to help people find games based on classes. I am using buttons with onclick="myFuntion()" but for some reason I cant seem to get it to work. I am pretty rusty with js but I think the logic is sound and should be working.

java script

function allFunction() {
    var filter = document.getElementById("game_filter");

    if (filter.classList.contains("games_all")) {
        element.classList.toggle("game_filter_show", true);
        element.classList.toggle("game_filter_hide", false);
    }
}

function fpsFunction() {
    var filter = document.getElementById("game_filter");

    if (filter.classList.contains("games_fps")) {
        element.classList.toggle("game_filter_show", true);
        element.classList.toggle("game_filter_hide", false);
    } else {
        element.classList.toggle("game_filter_show", false);
        element.classList.toggle("game_filter_hide", true);
    }
}

function survivalFunction() {
    var filter = document.getElementById("game_filter") {

    if (filter.classList.contains("games_survival")) {
        element.classList.toggle("game_filter_show", true);
        element.classList.toggle("game_filter_hide", false); 
    } else {
        element.classList.toggle("game_filter_show", false);
        element.classList.toggle("game_filter_hide", true);
    }
}

From what I can tell this should be working but its not and I am about to give up and just leave the filter out if i cant figure this out soon.

PS sorry if I have formatted this post wrong, im new to the site.

EDIT: Here is one of the HTML items I am trying to filter.

<div class="separator" 
id="game_filter"
class="game_filter_show"
class="games_all" 
class="games_fps" 
style="clear: both; text-align: center;">
<a class="GameList" href="http://foo.com" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://foo.png /></a></div>

Here is the html for the buttons.

<div style="text-align: center;">
<span style="font-size: large;">
<button onclick="allFunction()">All Games</button>
<button onclick="fpsFunction()">First Person Shooters</button>
<button onclick="survivalFunction()">Survival Games</button>
</span></div>
  • There is no declaration for 'element' and there is a extra "{" in the last var filter = document.getElementById("game_filter") {. Could you provide how you set class's on filter element? – kodvin Feb 04 '16 at 17:45
  • @kodvin here is one of the items on the filter list. – Immortal Pancake Feb 04 '16 at 17:57
  • all classes for one element should be placed in one attribute () class="separator game_filter_show games_all games_fps" – kodvin Feb 04 '16 at 18:10
  • like i said there is no "element" definition in element.classList.toggle("game_filter_hide", true); And i suspect you have multiple elements with same id – kodvin Feb 04 '16 at 18:12
  • Please look at second plunker and if it is not what you want you should try to rephrase what and how you want your filtering to work – kodvin Feb 04 '16 at 18:30

1 Answers1

1

Tested this code and it looks it works, unless you want something else to be done.

    <div class="games_survival" id="game_filter"></div>
    <div id="element"></div>

    <div onclick="allFunction()">all</div>
    <div onclick="fpsFunction()">fps</div>
    <div onclick="survivalFunction()">survivval</div>

<script>
var element = document.getElementById("element");
function allFunction() {
    var filter = document.getElementById("game_filter");

    if (filter.classList.contains("games_all")) {
        element.classList.toggle("game_filter_show", true);
        element.classList.toggle("game_filter_hide", false);
    }
}

function fpsFunction() {
    var filter = document.getElementById("game_filter");

    if (filter.classList.contains("games_fps")) {
        element.classList.toggle("game_filter_show", true);
        element.classList.toggle("game_filter_hide", false);
    } else {
        element.classList.toggle("game_filter_show", false);
        element.classList.toggle("game_filter_hide", true);
    }
}

function survivalFunction() {
    var filter = document.getElementById("game_filter") ;

    if (filter.classList.contains("games_survival")) {
        element.classList.toggle("game_filter_show", true);
        element.classList.toggle("game_filter_hide", false); 
    } else {
        element.classList.toggle("game_filter_show", false);
        element.classList.toggle("game_filter_hide", true);
    }
}
</script>

and here is a plunk to show how it works

Maybe you filter items depending on classes placed on them, then

   <style>
  .game_filter_show {color: red; } 
  .game_filter_hide{background-color: blue;}
  </style>

    <div class="element games_survival">text in here1</div>
    <div class="element games_fps">text in here2</div>

    <div onclick="allFunction()">all</div>
    <div onclick="fpsFunction()">fps</div>
    <div onclick="survivalFunction()">survivval</div>

<script>
function allFunction() {
var elements = document.getElementsByClassName("element");

      elements = Array.prototype.slice.call(elements,0);
    elements.map(function(element){
          element.classList.toggle("game_filter_show", true);
          element.classList.toggle("game_filter_hide", false);
    })
}

function fpsFunction() {
var elements = document.getElementsByClassName("element");
      elements = Array.prototype.slice.call(elements,0);
    elements.map(function(element){
      if (element.classList.contains("games_fps")) {
        element.classList.toggle("game_filter_show", true);
        element.classList.toggle("game_filter_hide", false);
    } else {
        element.classList.toggle("game_filter_show", false);
        element.classList.toggle("game_filter_hide", true);
    }    
    })
}

function survivalFunction() {
var elements = document.getElementsByClassName("element");
      elements = Array.prototype.slice.call(elements,0);
    elements.map(function(element){
      if (element.classList.contains("games_survival")) {
        element.classList.toggle("game_filter_show", true);
        element.classList.toggle("game_filter_hide", false); 
    } else {
        element.classList.toggle("game_filter_show", false);
        element.classList.toggle("game_filter_hide", true);
    } 
    })
}
</script>

and another plunker EDIT: remove games_all class checking, or you could add games_all o every item in the list, but this would be easier.

kodvin
  • 1,236
  • 9
  • 14
  • This Second one is what I am trying to do. I changed the styes to display: block and display: none and it worked like a charm. The one problem is that when I click on the all button it is supposed to make all of the divs show but nothing happens. Also what is the class="blue" for? – Immortal Pancake Feb 04 '16 at 18:53
  • ouh sorry, it was for testing. will remove it – kodvin Feb 04 '16 at 19:00
  • updated the answer and second plunker, you should either remove "games_all" checking or add this class to every element. I hope it helps – kodvin Feb 04 '16 at 19:06
  • Ok so i used the new version of the second code it it works fine the way you wrote it but when I implemented it on my blog it doesnt work at all. – Immortal Pancake Feb 04 '16 at 21:25
  • Update: Instead of working your code onto what I already had I just scrapped it and started from scratch with the example for the base and it is working now(well not on mobile but what ever). On another note I was just wondering if you had any ideas on how I could automatically sort the links once I start rating them so the higher the rating the closer to the top they would be. – Immortal Pancake Feb 05 '16 at 01:06
  • So if i helped please accept the answer and maybe plus it. And for sorting its another question and a duplicate one and you can find answers here http://stackoverflow.com/questions/282670/easiest-way-to-sort-dom-nodes – kodvin Feb 05 '16 at 06:40