Cyrill Cyrill - 1 year ago 46
Javascript Question

Using $(this) does not work

I am trying to get a script running that adds some events to a class.
I want these events only to be added to one specific DIV among this class. I am using pep.js:

$( ".drag" ).pep({
start: function() {

stop: function() {


This works, but it triggers all .drag items at onceā€¦ And I want only the one I am dragging to have added all the events.

I tried to write it with:

$(".drag", this).addClass('color');





But it all didn't work.
Does someone have an idea?


I made a JSFiddle that hopefully explains my problem.

As you see, the .color class for example is not only added to the DIV that is dragged, but also to the other. This is what I want to change.

Answer Source

Using console.log(arguments) in the event handlers, I was able to determine that the handlers are passed two arguments: the event object, and an object containing context. The context has a property $el, which is a jQuery object for the event target.

start: function(e,a) {
}, would also give you a reference to the element.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download