简体   繁体   中英

appending a div to a classname using javascript

var wrapper = document.getElementsByClassName('.wrapper');

for(var i =0; i < 10; i++){  //create grid of buttons

    var num = document.createElement("div");
    var node = document.createTextNode(i);
    num.appendChild(node);
    wrapper.appendChild(num);
    num.className = "number";

}

It throws an error that wrapper is not defined, it only worked when I used document.body instead of wrapper, but I want to append them divs to the wrapper div any suggestions ?

var wrapper = document.getElementsByClassName('wrapper');

instead of

var wrapper = document.getElementsByClassName('.wrapper');

EDIT:

var addButtons = function() {
 var wrapper = document.getElementsByClassName("wrapper")[0];
var num, node;
  for (var i = 0; i < 10; i++) { 
    console.info("Button"+i);
    num = document.createElement("div");
    num.className = "number";
    node = document.createTextNode(i);
    num.appendChild(node);

    wrapper.appendChild(num);

  }
}

addButtons();

https://jsfiddle.net/tonysamperi/7jzdsk08/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM