What’s the difference between jQuery $ and this.$ in Backbone.js View? in a View, I listened to a collection’s reset event to empty an element by using below code
//code in View
this.$("#the_id").empty();
however it did not empty my element, then I changed the code to
$("#the_id").empty();
this time it works. so, why this.$ does not do the work?
Calling
this.$( selector )in a view is same as calling$( selector, this )or$(this).find( selector ).It gives context for the selector I.E. only elements under
thisare searched to match the selector whereas normal jQuery$(selector)starts the search from the entire document’s root.Your view should not manipulate elements that it doesn’t own, if calling
this.$("#the_id").empty();doesn’t do anything it means that the element wasn’t under the view’s “area of control” or doesn’t exist to begin with.