Firstly, your example is correct, and secondly, as most people do. There is no keyword, just the reverse direction and intersection of the DOM for the most likely parent (which is stupid since you already found it, right?). In this case, I would recommend an intermediate variable, even if the other responder correctly showed how to use closest . (In addition, he relies on some technical information that <form> inside a <form> is invalid HTML. There is no guarantee for things like <div> that it does not cross any child <div> that is looking for next <a> .)
In jQuery you will see many things like this:
$("#parent").something(function() { var _this = this; // var self = this; // var that = this; // var someMoreMeaningfulName = this; $("#child", this).somethingElse(function() { $(_this).somethingEvenElser(); ... }); });
This is not the prettiest thing to read, but the pattern happens often enough to be easily recognized. I recommend a little what you do ( var form ), and not as the tag _this (underscore can mean private variables), self (actually it is not), etc.
brymck
source share