jQuery object when creating new elements


I’m creating new DOM elements with $() and it returns jQuery. Is [0] the best way to get the element itself?

var $iframe = $('<iframe></iframe').attr({
  id: this.id + '-iframe',
  frameBorder: '0'
  height: $this.height(),
  width: $this.width()
[B]var iframe = $iframe[0];[/B]


Typically it’s preferred to use the .get() method to retrieve elements from a jQuery object instead of digging straight in to the array index.

However, if you’re going to be adding the content to the page, you may want to leave it as an object and to use the .append() method, or [url=“http://api.jquery.com/appendto/”].appendTo() depending on which way you want to approach things.