0

I have several h2 elements on a page I'm working on and I am using waypoints.js to trigger an additional class to be added to them as they scroll into view.

Targeting each instance using ids works perfectly:

$('#one').waypoint(function(direction) { $(#one).addClass('newClass'); }, { offset: '80%' })

But I am trying to avoid using id on each instance as the content will likely be changed by the client on a regular basis so I would like to target only each individual h2 that scrolls into the viewport.

My thinking was:

$('h2').waypoint(function(direction) { $(this).addClass('newClass'); }, { offset: '80%' })

but that hasn't worked. Can someone help point me in the right direction here?

ric0c
  • 33
  • 5
  • `but that hasn't worked` How that?! – A. Wolff Dec 11 '15 at 14:35
  • 2
    Try it like this -> http://jsfiddle.net/e2sn8os9/ – adeneo Dec 11 '15 at 14:36
  • @adeneo looks like no each loop needed but must be: `$(this.element).addClass('newClass');` That's quite out of the way for a jQuery plugin... http://stackoverflow.com/questions/24060069/jquery-waypoints-using-multiple-classes#comment-37268443 – A. Wolff Dec 11 '15 at 14:39
  • @A.Wolff - yep, waypoints works without jQuery, but `context.js` is used to set the proper context when jQuery is used, and for some reason `this` is the plugins instance, while `this.element` is the elements it initialized on, and even stranger, `this.element.window` is the window the element is in. They sure are doing some strange things ? – adeneo Dec 11 '15 at 14:43
  • Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Dec 11 '15 at 15:19
  • @adeneo cool, that worked exactly how I wanted it to! If you offer that as an answer I'll select it and close this question. Thanks for the help! – ric0c Dec 11 '15 at 15:46

0 Answers0