Title: backbone.js el: 'jqueySelector' doesn't catch parent element
Tags: javascript;jquery;backbone.js;marionette
Question: I have the following code in a Marionette ItemView definition:
class AdProductDetailsView extends Marionette.ItemView
el: '#product-details'
template: =&gt;
price =
amount: if @model.get('price') then @model.get('price').get('amount') else 0
currency: if @model.get('price') then @model.get('price').get('currency') else 'BOB'
AdProductDetailsTemplate
price: Currency.format price.amount, price.currency
contact: @model.get('contact')?.toJSON()
location: @model.get('location')?.toJSON()
The thing is that the page doesn't "execute" the el: '#product-details' property. I've once ago tried to put it in the view's instantiation in another code, but I tried it and it didn't work. When I run this code, nothing appears on the screen, however, if I run it without el:..., the view is created and inserted in its own div, and it's this div wich I don't really want. Any ideas?
Comment: You probably need to show more code. As Patrick said, the `el` MUST be in the DOM when the view is being created, and it seems like that's not the case. How is the "#product-details" DOM created in your case?


Here is another answer: Could be a race condition, since you are assigning this view to #product-details, it needs to exist in the DOM before you instantiate that view.
This would explain why without el it works, because backbone creates a DOM element if one isn't defined.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:12/13/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><jquery><backbone.js><marionette>', 'Title': "backbone.js el: 'jqueySelector' doesn't catch parent element", 'ViewCount': 67, 'id': 20576813, 'user_AboutMe': None, 'user_AccountId': 3494447.0, 'user_DisplayName': 'Victor Hugo Rojas Alvarado', 'user_Reputation': 1.0, 'user_Views': 4.0, 'user_id': 3084771.0}}