0

Js: How to put created elements in order ...

Meaning elements created through JS - using document.createElement("div"); button.onclick = function(){ var mother= documnet.getEl...Id("parent"); for (var i = 0; i < 1;{ i++; var list = document.createElement("li"); mother.appendChild(list); list.innerHTML = someInput.value; var checkBox = document.createElement("input"); //imagine we put the attribute ... input type = checkbox; list.appendChild(checkBox); //inside the created lists is an input (checkbox); } } Now, how do I make sure that the checkbox is in front of the text (in front of list's text) The checkbox, then the text... // making a to-do list as you can tell.

25th Oct 2019, 2:38 PM
Ginfio
Ginfio - avatar
3 Réponses
+ 3
Ginfio I haven't tested yet, but as I understand it, elements are by default rendered in the order of theirs presence. So I was thinking, add an <ul> or <ol> element to 'mother'. Next add checkbox followed by the <li> element to the <ul> or <ol>. Just an idea.
25th Oct 2019, 3:19 PM
Ipang
+ 2
Use a label. Append the checkbox then append a "label" element with your text.
25th Oct 2019, 3:29 PM
Russ
Russ - avatar
+ 2
Try button.onclick = function(){ var mother= document.getElementById("parent"); for (var i = 0; i < 1;i++) { var list = document.createElement("li"); mother.appendChild(list); var checkBox = document.createElement("input"); checkBox.type = "checkbox" list.appendChild(checkBox); list.innerHTML += "<label>value </label>"; } }
25th Oct 2019, 3:33 PM
Calviղ
Calviղ - avatar