SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,109
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)

    jQuery object when creating new elements

    Hi.

    I'm creating new DOM elements with $() and it returns jQuery. Is [0] the best way to get the element itself?
    Code:
    var $iframe = $('<iframe></iframe').attr({
      id: this.id + '-iframe',
      frameBorder: '0'
    }).css({
      height: $this.height(),
      width: $this.width()
    });
    var iframe = $iframe[0];
    Cheers,

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by markbrown4 View Post
    Hi.

    I'm creating new DOM elements with $() and it returns jQuery. Is [0] the best way to get the element itself?
    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 .appendTo() depending on which way you want to approach things.

    $('#target').append($iframe);
    or
    $iframe.appendTo($('#target'));
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,109
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •