How to show and hide elements based on HTML data attributes
Asked Answered
U

5

7

HTML file:

<div class="test" data-word="AAA" data-type="T" data-number="1"> Text 1 </div>
<div class="test" data-word="AAA" data-type="BBB" data-number="2"> Text 2 </div>
<div class="test" data-word="AAA" data-type="CCC" data-number="2"> Text 3 </div>
<div class="test" data-word="D" data-type="BBB" data-number="6"> Text 4 </div>
<div class="test" data-word="AAA" data-type="BBB" data-number="2"> Text 6 </div>
<div class="test" data-word="G" data-type="R" data-number="5"> Text 7 </div>
<div class="test" data-word="H" data-type="BBB" data-number="1"> Text 5 </div>
<div class="test" data-word="AAA" data-type="R" data-number="9"> Text 8 </div>

I need to hide and show elements based on the attributes. Say I need to show elements with word AAA and type BBB and number 2. All the elements that have these attributes must be shown and others should be hidden using hide and show methods. Help?

Unmixed answered 22/12, 2014 at 19:56 Comment(0)
S
4

You can select the elements using the attribute equals selector:

$('.test').hide().filter('[data-word="AAA"][data-type="BBB"][data-number="2"]').show();

First hide all, then filter and show necessary. Check the demo below.

$('.test').hide().filter('[data-word="AAA"][data-type="BBB"][data-number="2"]').show();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test" data-word="AAA" data-type="T" data-number="1"> Text 1 </div>
<div class="test" data-word="AAA" data-type="BBB" data-number="2"> Text 2 </div>
<div class="test" data-word="AAA" data-type="CCC" data-number="2"> Text 3 </div>
<div class="test" data-word="D" data-type="BBB" data-number="6"> Text 4 </div>
<div class="test" data-word="AAA" data-type="BBB" data-number="2"> Text 6 </div>
<div class="test" data-word="G" data-type="R" data-number="5"> Text 7 </div>
<div class="test" data-word="H" data-type="BBB" data-number="1"> Text 5 </div>
<div class="test" data-word="AAA" data-type="R" data-number="9"> Text 8 </div>
Sapienza answered 22/12, 2014 at 19:58 Comment(0)
C
3

You could do this just based on selectors. For a more code-friendly approach use a filter function and read the data using jQuery data function. Start with the elements hidden and show the filtered results.

$('div.test').hide().filter(function(){
    var d = $(this).data();
    return d.word == 'AAA' && d.type == 'BBB' && d.number === 2;
}).show();

JSFiddle Example

Chara answered 22/12, 2014 at 20:0 Comment(2)
Why use filter when you can use CSS selectors?Psychogenic
As i mentioned it's a more dynamic and code friendly approach. You can use types and equality operators. It's a readability preference.Chara
P
1

$(function() {
  $(".test").each(function() {
    var d = $(this).data();
    $(this).toggle(d.word === "AAA" && d.type === "BBB" && d.number === 2);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="test" data-word="AAA" data-type="T" data-number="1">Text 1</div>
<div class="test" data-word="AAA" data-type="BBB" data-number="2">Text 2</div>
<div class="test" data-word="AAA" data-type="CCC" data-number="2">Text 3</div>
<div class="test" data-word="D" data-type="BBB" data-number="6">Text 4</div>
<div class="test" data-word="AAA" data-type="BBB" data-number="2">Text 6</div>
<div class="test" data-word="G" data-type="R" data-number="5">Text 7</div>
<div class="test" data-word="H" data-type="BBB" data-number="1">Text 5</div>
<div class="test" data-word="AAA" data-type="R" data-number="9">Text 8</div>
Psychogenic answered 22/12, 2014 at 20:7 Comment(0)
U
1
$('form')
    .children().data( { word: [ 'AAA','BBB' ], number: [2] } ).show();
Unheard answered 8/6, 2018 at 11:53 Comment(0)
C
0

try

$(".test").data("data-word")

to get the value you need and put on the logic

Cormier answered 22/12, 2014 at 20:3 Comment(1)
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Rabbitfish

© 2022 - 2024 — McMap. All rights reserved.