2

I want to break and center after each button, any suggestions? setAttribute did not work and does not add the breaks

for (var j = 0; j <= 6; j++) {
  var btn = document.createElement("BUTTON");
  var t = document.createTextNode(sm[j] + " " + sy[j]);
  btn.appendChild(t);
  document.body.appendChild(btn);
}
CHouse95
  • 111
  • 8
  • possible duplicate of [Creating Dynamic button with click event in javascript](http://stackoverflow.com/questions/7707074/creating-dynamic-button-with-click-event-in-javascript) – abc123 Jun 19 '15 at 14:45

1 Answers1

0

jsfiddle

HTML

<div id='theParent' class='center_the_stuff'>

</div>

JS

function addInput(type, value, name, id, onclick, parentId) {
    //Create an input type dynamically.   
    var element = document.createElement("input");
    //Assign different attributes to the element. 
    element.type = type;
    element.value = value; // Really? You want the default value to be the type string?
    element.name = name; // And the name too?
    element.id = id;
    element.onclick = onclick;

    var parent = document.getElementById(parentId);
    //Append the element in page (in span).  
    parent.appendChild(element);
}

function addBreak(parentId) {
    var br = document.createElement("br");
    var parent = document.getElementById(parentId);
    parent.appendChild(br);
}

window.onload = function () {
    for (var j = 0; j <= 6; j++) {
        var temp = 'mybutton' + j;
        addInput('button', temp, temp, temp, undefined, 'theParent');
        addBreak('theParent');
    }
}

CSS

.center_the_stuff {
    text-align: center;
}
abc123
  • 17,855
  • 7
  • 52
  • 82