Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

meaning of "this.el" in $('ul', this.el)

I am just going through some backbone tutorials and I have a general jQuery question that I have actually been wondering for a while.

Sometimes I see calls with a second parameter in the jQuery selector, for example $('ul', this.el).

What is the purpose of this second param in the selector? And I don't really mean in context of any backbone examples, just in general what is the purpose of passing the second param in the selector and why is it always an object that is passed there? I can't find any documentation on this.

like image 216
user834418 Avatar asked Sep 16 '11 20:09

user834418


People also ask

What is this El and this El?

$(this. el) creates a new instance of the jQuery/Zepto object. this. $el references a single instance of the former object.

What is El in HTML?

el is just an identifier and it refers to an element, a DOM element, which is a convention in that library.


2 Answers

The meaning is identical to:

$(this.el).find('ul')

Internally, after a bunch of tests, jQuery figures out that it needs to flip it around to the above .find() call, so it does, and starts over.

So providing the context as the second argument is just a slower way to do a .find().

Here it is in the source.

// HANDLE: $(expr, context)
// (which is just equivalent to: $(context).find(expr)
} else {
    return this.constructor( context ).find( selector );
}

...where this.constructor is the $ function, context is your second argument, and selector is your first argument.

like image 119
user113716 Avatar answered Sep 21 '22 05:09

user113716


It narrows the search for a ul tag within your view component's DOM element.

like image 22
Sam Dolan Avatar answered Sep 18 '22 05:09

Sam Dolan