Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    concat() method returning "not a function"

    If I get a collection of elements using the getElementsByTagName() method, and then get a second collection using the same method, shouldn't I be able to merge them using the concat() method?

    Example:
    Code:
    var d = document;
    var inputs = d.getElementsByTagName("input");
    var textareas = d.getElementsByTagName("textarea");
    var everything = inputs.concat(textareas);
    I am wanting to do something similar to the above to apply a common focus()/blur() behavior to these form elements, my thinking being that merging the collections before looping over them might lessen some overhead. But I keep getting "inputs.concat is not a function" from the Firefox JS console.

  • #2
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    concat only works for strings and arrays.

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I have encountered the same problem, take a look, it might help you:
    http://www.codingforums.com/showthread.php?t=47683
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    fci:
    I thought getElementsByTagName() returned at array.

    Kor:
    Thanks for the quick response and handy link!

  • #5
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    use typeof to find out what type a variable is. for example:
    Code:
    alert(typeof inputs);

  • #6
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Good tip, fci, thanks.

    Kor,
    I see you found a sort of workaround for your methods. How might I take two of these collections and create one, single-dimension array? So that, say, everything[0] through everything[10] are <input /> elements, and then everything[11] starts with the <textareas />?

  • #7
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    var d = document;
    var inputs = d.getElementsByTagName("input");
    var textareas = d.getElementsByTagName("textarea");
    function concat_collection(obj1, obj2) {
        var i;
        var arr  = new Array();
        var len1 = obj1.length;
        var len2 = obj2.length;
        for (i=0; i<len1; i++) {
            arr.push(obj1[i]);
        }
        for (i=0; i<len2; i++) {
            arr.push(obj2[i]);
        }
        return arr;
    }
    var blah = concat_collection(inputs,textareas);
    it has a length property similar to an array.


  •  

    Posting Permissions

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