Reputation: 1450
I am triggering a javascript based on a hash passed in a url.
But if the hash is not one that I am expecting I don't want to trigger.
The list of expected hashes is defined using the data-category
attribute.
How could I get an array of this data so I can see if my hash is in the array?
Here is the HTML I am given:
<div class="filter_padder" id="nav_channels">
<div class="filter_wrapper" style="max-width:650px;">
<div class="filter selected" data-category="cat-all">ALL</div>
<div class="filter" data-category="MusicWorld">MusicWorld</div>
<div class="filter" data-category="Awards">Awards</div>
<div class="filter" data-category="MX">Exchange</div>
<div class="filter last-child" data-category="Com">Community</div>
<div class="clear"></div>
</div>
</div>
Javascript:
// set any hash in the url to a variable
var hash = location.hash.replace('#', '');
// see if my hash is expected (would like to handle this dynamically)
if(hash === (cat_all|Awards|MX|Com)) {
doSomething();
}
But I don't know how to dynamically build the list of elements to compare to 'hash'. I think it's something along the lines of:
$('#nav_channels').data('category');
But can't get that to work.
Upvotes: 0
Views: 99
Reputation: 339796
You can get the values into an array like so:
var cats = $('[data-category]').map(function() {
return $(this).data('category');
}).get();
Upvotes: 0
Reputation: 10643
try this :
var hash = location.hash.replace('#', '');
if( $('.filter[data-category="'+hash+'"]').length ) {
doSomething();
};
Upvotes: 1