Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
Get data attribute from element and add class

This jquery code works and it is in use, but somehow my items filter not working with it correctly. But somehow my vanilla js working with that filter I use.

So my question is:

How I can change this jquery code to vanilla js?

$(".cat-name .cat-link").click(function () {

    let catID = $(this).data("id"),
        $allItem = $('.category')

    let $currentItem = $('.cat[data-category=' + catID + ']');
   
    $('.cat[data-category=' + catID + ']').addClass('active');
        $allItem.not($currentItem).removeClass('active');
    
    $('.all-items').removeClass('active'); // Remove active class from "All Items" tab
   
   });

Thanks if you have time to help me!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here is a quick answer :

  • use document.querySelector() and document.querySelectorAll() to select element(s)
  • use targetElement.addEventListener('click', function() { ... }) to add click event
  • use targetElement.className to add/remove classes
  • use targetElement.getAttribute('data-...') to manage data attributes

Remember to visit Vanilla JS website for more examples

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!