[jQuery] Odd behavior with 'this'?
Here's some behavior I've run into that is not quite what I expected.
So the question is, should I expect something different or is this a
bug / gotcha?
I'm working on a plugin for manipulating tables (the ones that are
published do not suit my needs).
So I have the following inside my plugin:
$.fn.myplugin = function(opts) {
do_something(this);
$(this).children('thead th:last').click(function() {
do_something_else(this);
};
};
function do_something_else(obj) {
my_clicked = $(obj).parents('thead');
....
};
An example table:
<table>
<thead>
<th>A</th><th>B</th>
</thead>
<tbody>
....
</tbody>
</table>
Now, I expect that my_clicked will be an object for <thead>, but in
actuality, it isn't anything, because obj is an object for <thead>,
not for <th>B</th> as I expect it to be (since that was what was
clicked, and should have been passed as 'this').
Could someone please explain why it's working that way to me, and how
I might get what I expect instead of what it is getting?