Crystal
Crystal

Reputation: 29458

this.$ in jquery / javascript

I'm just getting started in Javascript, Backbone and jQuery. I am running into some code that looks like this:

someBackboneFunction: function () {
    this.$('#index1').attr('disabled', 'disabled');
}

This function is declared in a backbone view subclass. From what I gather, this is referring to the function context of the object that gets created with this backbone view. The view that has this function declared is created with the new keyword. If that is correct, I'm not sure what

this.$('#index1')

actually means. Does it just mean that on the object itself that was created with the new keyword, find the index1 id and disable it? Thanks!

Upvotes: 0

Views: 76

Answers (2)

gpasci
gpasci

Reputation: 1440

From what I remember in Backbone this.$ is a wrapper for jQuery and it allows you to select elements in the scope of your view.

So this.$('#index1') should mean "select #index1 in my view" and .attr('disabled', 'disabled') sets its disabled attribute to true.

Upvotes: 0

Stephen
Stephen

Reputation: 5770

this.$('#index1') is finding an element that matches the selector, #index1, as a child of the view's element itself.

For use within a view, there will be a this.$el that represents the view's element, wrapped as a jQuery object. this.$('selector') is the same as this.$el.find('selector').

Upvotes: 2

Related Questions