6

I just need a little tweak, I know all this code isn't perfect (far from it). Instead of writing the contents of the array into a div, I ideally want to create a new div for each number in the array and then add it to the card container?

balls90 = [ '1', '2', '3', '4', '5', '6','7','8','9','10','11', '12', '13', '14', '15', '16','17','18','19','20','21', '22', '23', '24', '25', '26','27','28','29','30','31', '32', '33', '34', '35', '36','37','38','39','40','41', '42', '43', '44', '45', '46','47','48','49','50','51', '52', '53', '54', '55', '56','57','58','59','60','61', '62', '63', '64', '65', '66','67','68','69','70','71', '72', '73', '74', '75', '76','77','78','79','80','81', '82', '83', '84', '85', '86','87','88','89','90' ];

function getNumbers(){

    var player1 = new Array();

    balls90.sort( function() { return Math.random() - .25 } );

    for ( var i=1; i<=12; i++ ) {   
        player1.push(balls90[i]);
        document.getElementById("cardContainer").innerHTML+=(balls90[i]);
    }

}
1
  • can u explain more what u need? Commented Apr 4, 2011 at 15:34

9 Answers 9

11

Simple method, instead of appending to the div try appending to a variable, something like

var html='';
for (var i=1; i<=12; i++) {
    html+='<div>'+balls90[i]+'</div>';
}
document.getElementById('cardContainer').innerHTML+= html;
Sign up to request clarification or add additional context in comments.

Comments

4

You can easily use document.createElement("div") and appendChild()

var div = document.createElement("div");
div.innerHTML = (balls90[i]);
cardContainer.appendChild(div);

Example on jsfiddle.

Using this method will get you away from having to worry about yucky string concatenation.

Comments

1

You can accomplish this with element.createChild() and element.appendChild().

Comments

1

Try this

                var full_list = "";
                for(var i=0; i<balls90.length; ++i){
                    full_list = full_list + balls90[i]+ "<br>";
                } 

                $("#cardContainer").html(full_list);

Comments

1

Dynamically create a div with var div = document.createElement('div') and append the array iteration (values) to a dynamically created node var node = document.createTextNode(value[i]) . Just store the dynamically created DOM-Element into a variable like var div. You can do the same to the dynamically created node. Then you can append the node to the DOM-Element with div.appendChild(node) and append the whole to the Parent Element.

Comments

0
document.getElementById("cardContainer").innerHTML += ('<div>'+balls90[i]+'</div>');

If you want each number in its own div, just wrap each number in <div></div>

jsFiddle: http://jsfiddle.net/UjsgY/

Comments

0

Like this?

balls90 = [];
for (var i=0;i<90;i++)balls90[i]=(i+1);

function getNumbers(){
  var player1 = new Array();
  balls90.sort( function() { return Math.random() - .25 } );
  var container = document.getElementById("cardContainer")
  var div;
  for ( var i=1; i<=12; i++ ) { 
    player1.push(balls90[i]);
    div = document.createElement('div');
    div.innerHTML=balls90[i];
    container.appendChild(div)
  }
}

Comments

0

If I understand correctly, you want to create a div for each element in the array. You may (not) be familiar with array.join(). You will need a container to put the contents in, I will create one then create the individual divs with array.join().

var container = document.createElement('div');
//A string containing a series of divs and the contents of the Array.
container.innerHTML = '<div>' + balls90.join('</div><div>') + '</div>';  
//append div to body last, to create less DOM refreshes
document.body.appendChild( container ); 

Comments

0

// Lots of ways to do this, here's another

var div= document.createElement('div'),
txt= document.createTextNode('1'), i= 1, tem,
pa= document.createDocumentFragment();
div.appendChild(txt);
pa.appendChild(div);
while(i<90){
    tem= div.cloneNode(true);
    tem.firstChild.data=++i;
    pa.appendChild(tem);
}
document.getElementById("cardContainer") .appendChild(pa);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.