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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With