SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    split, am i doing something wrong?

    This works
    Code JavaScript:
    var myVariable = "a,b,c,d";
    var stringArray = myVariable.split(",");
     
    document.write(stringArray[0]);
    document.write(stringArray[1]);
    document.write(stringArray[2]);
    document.write(stringArray[3]);


    But I cant figure out why this wont:

    Code JavaScript:
    var List = "First,Second,Third";
    var Full = List.split(",");
     
    document.write(Full);

    anyone see what Im doing wrong? (no firebug errors)

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What happens if you alert(Full) ??
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  3. #3
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It should work.

    You might want to loop through the array though.

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    String.split returns an array. document.write expects a string.

  5. #5
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    String.split returns an array. document.write expects a string.
    In most cases it should work though. It will automatically run array.toString()

  6. #6
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As M-M-J noted, I believe it did run toString of the Array.prototype when I did a quick test in Firebug's console.


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
  •