Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refer to Backbone view's child element

Having just rendered a view like so:

render: function() {
    this.$el.html(this.template(this.model.toJSON()));
    return this;
},

How can I refer to one of the template's child elements and apply a jQuery function to it?

like image 679
el_pup_le Avatar asked Jul 20 '13 23:07

el_pup_le


1 Answers

Backbone Views expose a dollar $ function that will use jQuery under the covers, but within the context of the view itself.

this.$('.child_element_of_my_view_template')

This will work even if the view is detached ($el not in DOM) but will obiouvsly only work by the time the element you want to select exists within the view (appended to $el).

This means that you can safely use it after the first line of your render function.

like image 90
namero999 Avatar answered Nov 04 '22 21:11

namero999