Search code examples
javascriptjquerybackbone.jsmarionette

Whats the meaning of $('element',this.el)[0]?


Now I learning Backbone and Marionette, I read some tutorial and I found a code whom I haven't understand. Here's the code:

$('element',this.el)[0]

I know jQuery little bit. I know this keyword, i know the $('element') keyword, but not understand that code, please everybody tell me about that.


Solution

  • This $('element',this.el) says select all <element> contained within this.el. this.el must be another "object" but what it is depends on what is building this higher up. I cover this in more detail in this answer to a similar question.


    The [0] simply unwraps the jquery object returning a vanilla DOM object. So:

    $('element',this.el).first(); //works
    $('element',this.el)[0].first(); //will error
    

    The second errors becuase it is no longer a jquery object so it is not wrapped in the jquery functions.