1

I've got a problem that I can't figure out and was wondering if you good people could help out? I'm building a filter system that uses data options on the tags.

The nav elements add to an array when pressed and take that option out of the array when pressed again.

You may notice that the first set allows for combination and the date range doesn't. This is intentional. My problem lies with asking the script to show the elements in the #container that match the data tag when pressed - I want to show the li elements within #container that match the data-season="" or the data-date="".

in the seasons script this is my problematic piece of script....

if (typeof $("#container li").data('season' == showseason ) ) { $(this).show(); } 

I've tried various ways but I'm now just going in loops getting more confused with each attempt. HELP :)

Jsfiddle Demo

2
  • 4
    I think you should really have researched what typeof does, rather than just used it because it (presumably) 'sounded right'. Also, the equality operator inside of the data() method means you're looking for a data-true/data-false (I think) variable, not assessing the returned attribute-string. Please: check the documentation when something doesn't work. Incidentally, when posting a problem, post a minimal demo, in order to get help. Commented Aug 17, 2014 at 11:10
  • I've now shortened it as much as possible jsfiddle.net/daviemurphy/b2eh2w07/10 Commented Aug 17, 2014 at 11:25

1 Answer 1

1

You should change that if statement. remove the typeof keyword, and compare the data value.

 if ($("#container li").data('season') == showseason ) { // do something here } 

Or better yet, iterate through each of the li within #container and get the data-season value.

$("#container li").each(function(){ var season = $(this).data("season"); if(season == showseason) $(this).show(); else $(this).hide(); }); 

Please refer to the updated fiddle: http://jsfiddle.net/b2eh2w07/11/

Sign up to request clarification or add additional context in comments.

15 Comments

this will be faster than $(this)
Hi, cheers for the help but I'm not getting any results within the #container still. Is my selector wrong perhaps?
if ($("#container li").data('season') == showseason ) { $("#container li[data-season=]" + showseason).show(); } Something like this, albeit the [data-season=] + showseason isn't quite right?
You can simplify (and maybe even speed up) your code by using $("#container li").hide(); and $("#container li[data-season='" + showseason + "']").show();
@Regent Even better, $("#container li").hide().filter("[data-season='" + showseason + "']").show();
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.