javascript - How to add an element to another one(instead of using selector) -


i found in document can create element , add 1 like:

var sel = document.createelement("select"); var opt1 = document.createelement("option"); var opt2 = document.createelement("option");  opt1.value = "1"; opt1.text = "option: value 1";  opt2.value = "2"; opt2.text = "option: value 2";  sel.add(opt1, null); sel.add(opt2, null); 

but when tried apply practice, method doesn't work, newuser not added newdiv successfully:

function createonele(imgsrc, user, extract) {   var newdiv = document.createelement("div");   var newuser = document.createelement("span");    newuser.textcontent = user;    newdiv.add(newuser);  } 

it seems add method doesn't work div , span, if true, how achieve it? otherwise, did wrong?

.add method of select-element used add option-element it.

refer htmlselectelement.add()

use .appendchild()

the node.appendchild() method adds node end of list of children of specified parent node.

function createonele(user) {    var newdiv = document.createelement("div");    var newuser = document.createelement("span");    newuser.textcontent = user;    newdiv.appendchild(newuser);    document.body.appendchild(newdiv);  }  createonele('rogb :)')


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -