1

I want to hide certain rows in my table when I click a button as well as retain certain rows. For example, I clicked "Show Fire-based champs" button. It will only show champs that have the Fire attribute and will hide the rest (from the same table). I clicked another button called "Show Water-based champs" that will only show champs that have the Water attribute and will hide the rest etc. and will be the same for other buttons. Anyone can make a code for it? I'm not experienced in JS.

Here is how the main table looks like (just a sample, there are a lot more table rows).

<table border="1" width="800">
<thead><tr>
    <th>Hero</th>
    <th>Class</th>
    <th>Offensive Skill</th>
    <th>Passive Skill</th>
    <th>Ultimate</th>
</tr></thead>
<tbody>
    <tr><td><a class="holy">Arcana</a></td>
    <td>the Arcane Manipulator</td>
    <td> - </td>
    <td> - </td>
    <td>Arcane Destroyer</td></tr>

    <tr><td><a class="fire">Azakiel</a></td>
    <td>the Blood Mage</td>
    <td> - </td>
    <td>Elf Blood</td>
    <td>Call of the Phoenix</td></tr>

    <tr><td><a class="wind">Bahamut</a></td>
    <td>the King of the Skies</td>
    <td> - </td>
    <td> - </td>
    <td>Mega Flare</td></tr>

    <tr><td><a class="dark">Carinblack</a></td>
    <td>the Dark Assailant</td>
    <td>Sword Bash</td>
    <td> - </td>
    <td>Blade of the Dark</td></tr>

    <tr><td><a class="earth">Dran</a></td>
    <td>the Steel Beast</td>
    <td>Rushing Tackle</td>
    <td> - </td>
    <td>Rolling Thunder</td></tr>

    <tr><td><a class="water">Fenrir</a></td>
    <td>the Water Emperor</td>
    <td>Water Barrage</td>
    <td> - </td>
    <td>Waterfall</td></tr>

    <tr><td><a class="thunder">Larza</a></td>
    <td>the Lightning Heroine</td>
    <td>Staff of Lightning</td>
    <td> - </td>
    <td>Storm Surge</td></tr>

    <tr><td><a class="thunder">Razor</a></td>
    <td>the Thunder Emperor</td>
    <td>Thunder Strike</td>
    <td> - </td>
    <td>Thunderstorm</td></tr>



0

1 Answer 1

2

Use show() and hide()

For example, when "Show fire-based champs" is clicked, do:

$('tbody tr').hide() //Hide all rows
$('tbody tr:has(a.fire)').show() //Show all fire rows

Here is a jsfiddle that has all the code in it

HTML code:

<html><head>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.0b1.js"></script>
<script type="text/javascript">

 function show(champ){
  $('tbody tr').hide()   
  $('tbody tr:has(a.'+champ+')').show()
 }


</script>


</head>
<body>
  <table border="1" width="800">
<thead><tr>
    <th>Hero</th>
    <th>Class</th>
    <th>Offensive Skill</th>
    <th>Passive Skill</th>
    <th>Ultimate</th>
</tr></thead>
<tbody>
    <tr><td><a class="holy">Arcana</a></td>
    <td>the Arcane Manipulator</td>
    <td> - </td>
    <td> - </td>
    <td>Arcane Destroyer</td></tr>

    <tr><td><a class="fire">Azakiel</a></td>
    <td>the Blood Mage</td>
    <td> - </td>
    <td>Elf Blood</td>
    <td>Call of the Phoenix</td></tr>

    <tr><td><a class="wind">Bahamut</a></td>
    <td>the King of the Skies</td>
    <td> - </td>
    <td> - </td>
    <td>Mega Flare</td></tr>

    <tr><td><a class="dark">Carinblack</a></td>
    <td>the Dark Assailant</td>
    <td>Sword Bash</td>
    <td> - </td>
    <td>Blade of the Dark</td></tr>

    <tr><td><a class="earth">Dran</a></td>
    <td>the Steel Beast</td>
    <td>Rushing Tackle</td>
    <td> - </td>
    <td>Rolling Thunder</td></tr>

    <tr><td><a class="water">Fenrir</a></td>
    <td>the Water Emperor</td>
    <td>Water Barrage</td>
    <td> - </td>
    <td>Waterfall</td></tr>

    <tr><td><a class="thunder">Larza</a></td>
    <td>the Lightning Heroine</td>
    <td>Staff of Lightning</td>
    <td> - </td>
    <td>Storm Surge</td></tr>

    <tr><td><a class="thunder">Razor</a></td>
    <td>the Thunder Emperor</td>
    <td>Thunder Strike</td>
    <td> - </td>
    <td>Thunderstorm</td></tr>
    </tbody>
</table>
<button onclick="show(&quot;fire&quot;)">Show fire</button>
<button onclick="show(&quot;water&quot;)">Show water</button>
<button onclick="show(&quot;thunder&quot;)">Show thunder</button>
<button onclick="show(&quot;dark&quot;)">Show dark</button>
<button onclick="show(&quot;earth&quot;)">Show earth</button>
<button onclick="show(&quot;holy&quot;)">Show holy</button>
<button onclick="show(&quot;wind&quot;)">Show wind</button>
<button onclick="$('tbody tr').show()  ">Show all</button>
</body></html>
Sign up to request clarification or add additional context in comments.

6 Comments

Do I need to replicate the same script? eg. codefasfasfs <script type="text/javascript"> function show(water){ $('tbody tr').hide() $('tbody tr:has(a.'+water+')').show() } </script> <script type="text/javascript"> function show(fire){ $('tbody tr').hide() $('tbody tr:has(a.'+fire+')').show() } </script> code
@Ralphx3589: nah, the function calls in the button take care of this.
So do I just copy the HTML code as is? No more editing? EDIT: oh thanks! It works as I intended it to be! Thank you so much!
@Ralphx3589: yep. If it worked, hit the green checkmark next to my answer to "accept" it :)
Wait, it works on the fiddle thing but when I placed it on the notepad, the buttons doesn't work. Am I missing something?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.