Search code examples
javascriptjqueryhtmljquery-waypoints

jQuery addClass to element with same class name with Waypoints


I have an event in which I need to add a class to an element with a matching class name. For example:

<a class="one"></div>
<a class="two"></div>

<div class="one"></div>
<div class="two"></div>

How do I find and add an additional class to the element with the matching class name? Here is my script, I need it to target the tag with matching class.

jQuery('div.two').waypoint(function(direction) {
  if (direction === 'down') {
    jQuery(this).addClass("active") // to <a> element that shares same class
  }
  else {
  }
});

Solution

  • I'm not familiar with this plugin but I'll give it a shot. Based off what you've provided I think your problem is:

    jQuery(this).addClass("active")
    

    Since you already know the class, just do:

    var tempClass = $(this).attr("class");
    jQuery("a."+tempClass).addClass("active");