www.webdeveloper.com
Results 1 to 5 of 5

Thread: Array talk

  1. #1
    Join Date
    Aug 2007
    Location
    Germany, NRW
    Posts
    76

    Question Array talk

    Hi,

    playing arround with arrays i discovered two different kinds of arrays.

    Have look on this code:
    Code:
    var temp = gallery_list.getElementsByTagName('dd');
    var tumb = new Array();
    for(var i = 0; i < temp.length; i++){
    	if(temp[i].className == 'view_button')
    	   tumb.push(temp[i]);
    }
    view_button_list = tumb;
    The temp array is as you can see provided by the 'getElementsByTagName'-Method.
    The tump array is put together by a custom loop.

    If i look with firebug at the arrays, i get different readings(see attached image01.jpg). While 'temp' got its length property and its unique methods.
    'tumb' does not. How come? Whats the difference?

    And how do i make a custom element array like one that is created by getElementsByTagName.

    Hope you can help out.

    Greets

    Jens
    Attached Images Attached Images

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    temp is not an array, its a dom collection.

    i don't think you can make one on your own, but then again, why on earth would you want a slow feature-less array?

  3. #3
    Join Date
    Aug 2007
    Location
    Germany, NRW
    Posts
    76
    he thx new keyword!

    I tested a little bit more and i couldn't find any differences in behavior.
    Anything i need to be aware of when handling Dom Collections?

    Flanagan stated that Dom api method getElementsByTagName returns the found Elements in form of an array.

    So why are there differences in the firebug debig display mentioned above.?

    Greets

    Jens

  4. #4
    Join Date
    Feb 2006
    Posts
    2,926
    It is more common to convert a node collection to an array.

    Code:
    var A=[],tags=document.getElementsByTagName('dd');
    A=A.slice.call(tags,0,tags.length);
    A is an array of the elements in the tags node list.
    You can use any array methods to manipulate the array,
    but it is no longer live, as a node list is.

    It is a 'snapshot' of the document at the time the array was created.

  5. #5
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by jens1701 View Post
    Flanagan stated that Dom api method getElementsByTagName returns the found Elements in form of an array.

    So why are there differences in the firebug debig display mentioned above.&#180;?
    I don't know who Flanagan is, but they are mistaken.

    None of the array methods are available on collections.

    If you want use Array.map for example, you need to convert it into an array first.

    mrhoo's method works in FireFox, but won't work in IE.

    A cross browser method:


    Code:
    function obValsLen(ob){var r=[],i=0,mx=ob.length;for(var z=0;z<mx;z++){r[z]=ob[z];}return r;}
    
    //usage example: 
    array=obValsLen(document.getElementsByTagName("div"));
    Last edited by rnd me; 06-15-2009 at 03:56 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles