-4

I found a demo with a filter and sort function that has almost all I need. The problem isn’t that I want to sort them under each other.

You guys can see it here what I mean: http://1337zone.WIN

These boxes under the filter buttons need to be under each other, not next to each other. They did it in JavaScript I think. When I delete JS files it doesn’t work at all.

The best option is to do it myself. I was searching it for a long time and I just gave up — I don’t understand JavaScript.

It basically look like this:

<div class="grid">
  <div class="element-item metal transition green" data-category="transition">
    <h5 class="name">Hallo</h5>
    <p class="symbol">7</p>
    <p class="number">153</p>
    <p class="weight">53</p>
  </div>
  <div class="element-item metalloid blue" data-category="metalloid">
    <h5 class="name">Hello</h5>
    <p class="symbol">1</p>
    <p class="number">521</p>
    <p class="weight">12</p>
  </div>
  <div class="element-item metal post-transition cyan" data-category="post-transition">
    <h5 class="name">Hi</h5>
    <p class="symbol">3</p>
    <p class="number">83</p>
    <p class="weight">20</p>
  </div>
  <div class="element-item metal post-transition cyan" data-category="post-transition">
    <h5 class="name">Was?</h5>
    <p class="symbol">3</p>
    <p class="number">82</p>
    <p class="weight">20</p>
  </div>
</div>

In the class you can see "cyan", "blue", "green" and this code:

<button class="button" data-filter=".green">green</button>

does some magic and makes it work. Don’t ask me how because I have no idea how.

Is it possible to do it in HTML and CSS only?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
1337zone.WIN
  • 11
  • 1
  • 4

1 Answers1

0

This can be done without using a plugin in the following way. The value of data filter is get using data() function in jQuery and use that to show items with that class name.

$(document).ready(function() {
  $('.button').on('click', function() {
    var classSelector = $(this).data('filter');
    $('.grid .element-item').hide();
    $('.grid').find(classSelector).show();
  });
});
.green{
  background-color:green;
}
.cyan{
  background-color:cyan;
}
.blue{
  background-color:blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="grid">
  <div class="element-item metal transition green" data-category="transition">
    <h5 class="name">Hallo</h5>
    <p class="symbol">7</p>
    <p class="number">153</p>
    <p class="weight">53</p>
  </div>
  <div class="element-item metalloid blue" data-category="metalloid">
    <h5 class="name">Hello</h5>
    <p class="symbol">1</p>
    <p class="number">521</p>
    <p class="weight">12</p>
  </div>
  <div class="element-item metal post-transition cyan" data-category="post-transition">
    <h5 class="name">Hi</h5>
    <p class="symbol">3</p>
    <p class="number">83</p>
    <p class="weight">20</p>
  </div>
  <div class="element-item metal post-transition cyan" data-category="post-transition">
    <h5 class="name">Was?</h5>
    <p class="symbol">3</p>
    <p class="number">82</p>
    <p class="weight">20</p>
  </div>
</div>

<button class="button" data-filter=".green">green</button>
<button class="button" data-filter=".cyan">cyan</button>
<button class="button" data-filter=".blue">blue</button>
rrk
  • 15,677
  • 4
  • 29
  • 45
  • right ... but question was "Is it possible to do it ONLY in HTML and CSS ?" answer is NO – daremachine Sep 12 '15 at 19:34
  • @daremachine check the OP's comments. – rrk Sep 12 '15 at 19:35
  • i checked but comments are not question =) but your solution is clean and good – daremachine Sep 12 '15 at 19:37
  • I understand this script a little it's easier than i thought, the script from the demo is so long that i was shocked and i couln't breath. Coud you please explain me the part from Var to show ? – 1337zone.WIN Sep 12 '15 at 19:43
  • @1337zoneWIN `$(this).data('filter')` gets the value of the clicked button's `data-filter` attribute value. Which is like `.green` and that is the jquery selector for class `green`. – rrk Sep 12 '15 at 19:49
  • @1337zoneWIN then we hide all `$('.grid .element-item').hide();` divs. Then show only the divs with class you selected using `$('.grid').find(classSelector).show();` the resultant of this line will be like `$('.grid').find('.green').show();` – rrk Sep 12 '15 at 19:51
  • Okay i understand, wow you explained it so easy that even i understood it O.o So if i want to show ALL do i need to make it like: for the easiest way ? I have also an other problem, i uploadet in on my FTP and it don't work somehow :( – 1337zone.WIN Sep 12 '15 at 20:17
  • @1337zoneWIN if you put `data-filter=".element-item"`, then it will show all the elements on click of the button. – rrk Sep 12 '15 at 20:22
  • Hi i think i broke something. http://1337zone.win/ – 1337zone.WIN Sep 12 '15 at 20:37
  • I add the script, made files and hmm it was working some time and then it somehow stop :( – 1337zone.WIN Sep 12 '15 at 20:42
  • @1337zoneWIN that is because you haven't included jQuery library. – rrk Sep 12 '15 at 20:43
  • @1337zoneWIN https://learn.jquery.com/jquery-mobile/getting-started/ – rrk Sep 12 '15 at 20:45
  • I guess this did it: – 1337zone.WIN Sep 12 '15 at 20:45
  • @1337zoneWIN add that before you added ``, and see the change I made in the answer. use that `$(document).ready(function() {`. – rrk Sep 12 '15 at 20:53
  • When i add this $(document).ready(function() { it doesn't wor even on fiddle. Without $(document).ready(function() { it works on fiddle. It was working for some time and then it just STOP working dont know why. – 1337zone.WIN Sep 12 '15 at 20:59
  • I have it like this. https://jsfiddle.net/1337zoneWIN/54pbeLq1/3/ And it works here but not on FTP lolz – 1337zone.WIN Sep 12 '15 at 21:03
  • It works now i need to change it from: to Sorry for being annoying just sorry, how can i sort things ? With this:

    7

    153

    53

    – 1337zone.WIN Sep 12 '15 at 21:13
  • @1337zoneWIN add new questions for out of this question's scope. http://stackoverflow.com/questions/12073270/sorting-options-elements-alphabetically-using-jquery this might help you. – rrk Sep 12 '15 at 21:27
  • It's again me, how can i edit the javascript that i can use 2 buttons ? i mean that i can select two filters at the same time "green" and "blue" and it shows me green and blue without cyan – 1337zone.WIN Sep 13 '15 at 00:54