add a class to element with specific data-filter attribute?

I’m in need of a bit of a jQuery hack for this WordPress project. It’s generating links with data-filters but no unique IDs or anything.

<a data-filter=".term73" href="#">

And I’d like to manually tell .term73 to have class “active” when the page loads. I’ve used the addClass() function before, but can’t seem to find how to do that to an element by data-filter (as opposed to ID). Ideas?

Related posts

Leave a Reply

1 comment

  1. Like this?

    $('a[data-filter=".term73"]').addClass('active');
    

    or even

    $('a[data-filter=".term73"][href="#"]').addClass('active');
    

    just to point out how you can target attributes.

    While I’m at it, an alternative which might be good to know, but overkill for this scenario:

    $('a[href="#"]').filter(function(){
        return $(this).data('filter') === '.term73';
    }).addClass('active');