Sunday, July 29, 2012

jquery constructor with context $(selector,this)

Inside the jquery event handler, quite often we need to select a sub element within the passed in event element, and do some operation on it. The passed in event element is accessible from the this object. That is the place you will see a lot of $("selector", this) get called.

$(selector, this) is another jquery constructor, the second parameter takes a context variable. In this case, instead of selecting the elements from the current document, it just selects the matched elements from the context parameter. In the event handler, "this" represents the DOM object that the current event happens on it, to use the jquery method on it, we need first converts it to jquery object with $(this).

No comments:

Post a Comment