www.webdeveloper.com
Results 1 to 7 of 7

Thread: Help splitting a string

  1. #1
    Join Date
    May 2014
    Posts
    3

    Help splitting a string

    I got this code from online and it works perfectly from the commented line down. It shows each word in a div. I added the array etc above the commented part but when I run that it shows every word in a div.

    I have no idea why.

    Code:
        var carArray = ['Saab', 'Volvo', 'BMW', 'GMC', 'Nissan', 'Ford'];
        var cars = carArray.toString();
        cars.split(",");
    
        //var cars = 'Saab,Volvo,BMW,GMC,Nissan,Ford'.split(",");
        for (var c in cars)
    
        {
            var newElement = document.createElement('div');
            newElement.id = cars[c]; newElement.className = "car";
            newElement.innerHTML = cars[c];
            document.body.appendChild(newElement);
        }

  2. #2
    Join Date
    May 2014
    Posts
    3
    I can't edit so one error, when I run my version it puts every letter into a new div.

  3. #3
    Join Date
    May 2014
    Posts
    3
    Sorted it by creating a new variable in the middle then passing that variable to cars so it now became

    Code:
    var carArray = ['Saab', 'Volvo', 'BMW', 'GMC', 'Nissan', 'Ford'];
        var carsToSplit = carArray.toString();
        var cars = carsToSplit.split(",", 2);

  4. #4
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,472
    Hi there macnasty,

    and a warm welcome to these forums.

    You seem to be over-complicating it a little bit.

    This would do the job...
    Code:
    
       var cars=['Saab','Volvo','BMW','GMC','Nissan','Ford'];
    
    for(var c in cars) {
       var newElement=document.createElement('div');
           newElement.id=cars[c]; 
           newElement.className='car';
           newElement.innerHTML=cars[c];
           document.body.appendChild(newElement);
     } 

    coothead

  5. #5
    Join Date
    Nov 2010
    Posts
    1,090
    you shouldn't really be using a for... in loop for arrays - they are better suited to objects. Traditionally, arrays have been iterated over using the incrementing for loop, or you can use the funky newish forEach method (modern browsers only)...
    Code:
    <script>
    var carArray = ['Saab', 'Volvo', 'BMW', 'GMC', 'Nissan', 'Ford'];
    
    carArray.forEach(function (val) {
            var newElement = document.createElement('div');
            newElement.id = val; 
    	newElement.className = "car";
            newElement.innerHTML = val;
            document.body.appendChild(newElement);
    });
    
    </script>

  6. #6
    The code you have makes a DIV for each word and plugs the word into it -- what were you expecting it to do?

    On top of the bizarre redundancies, use of 'in' on an array, use of innerHTML contrary to us being told not to anymore...

    I'm just not understanding what you are expecting it to do OTHER than make one DIV for each word? Do you want all the words just slapped into one DIV? Are you looking for some other tags inside a DIV or something?

  7. #7
    Join Date
    May 2014
    Posts
    10
    I think, you cannot split it.

Thread Information

Users Browsing this Thread

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

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