What does $(which); do?!?!?

I’m reading the Jquery Novice to Ninja book and on page 268 where there is this unexplained snippet of code containing a selector $(which);

I do not know what this means. Reading this book is FRUSTRATING as I have to keep a laundry list of things I got to research when something is not explained, but the string $(which); is NOT searchable as it seems to be mis-interpreted so I have to ask here. What does this piece of code mean???!?!??

Ok, on fourth read, I note that the text says its’s storing our what the user has started to drag, but I would like to see some further explanation about what is the difference between this and $(which) and how do I know which of these I’m supposed to use?

The over all effect of having foreign syntax used without explanation is like taking a foreign language course where words are used without defining them in sentences and you have to guess what the word means and of course like the Indiana Jones movie where they have to chose the right cup out of many, the chances of a wrong guess and needed unlearning are great!

Thanks kindly, just like the others you were correct, but you found the exact place where which was declared. I am like another here, unimpressed with the choice of variable names here. I would have used something like selectedElement or selectedItem which would be far more easily recognized as a variable name rather than some form of control statement keyword as some languages use that word for in syntax like switch and case statements.

“which” is probably defined elsewhere as a selector string, like ‘#something > p:first-child’, and that’s what is being passed to the $ funciton.

The only certainty is that “which” is a variable which was defined elsewhere.

It’s hard to say without the context what’s going on.

Looks like it’s redundant anyway. If which is a jQuery object already (through using $), they could just have done this:

which.fadeOut(200);

unless in between those lines “which” is turned into the actual DOM element (using get()), in which case it would have to be turned back into a jQuery object using $.

In the example, “which” is just a javascript var. It certainly would not be my choice to use as an example.

For example, it looks like it’s used in this manner:


var which = $('#someid div p.someclass');

$(which).fadeOut(200);

Look on page 266 and at the function puffRemove(). puffRemove() is getting “ui.draggable” passed into the “which” variable (the contents of the puffRemove() function are on page 268)

I tend to use el for elements, and when it’s a jquery object I prefix the variable with dollar, so it would be $el for jquery reference to an unknown element. That way when you come back to the code some 6 months later, you instantly know that $el is supposed to be a jquery reference to an element.

That’s the most generic I would go with that type of variable name. There’s always room to make the variable name more explicit, where that makes the code easier to understand.