-1

I managed to add an input(text-field of login) childNode to a label(login)

<script>
  let wrapperr = document.getElementById("dynamic-field");

  var label1 = document.createElement("label");
  label1.appendChild(document.createTextNode("login"));
  wrapperr.appendChild(label1);

  linebreak = document.createElement("br");
  label1.appendChild(linebreak);

  var input1 = document.createElement("input");
  label1.insertBefore(input1, label1.children[1]).style.width = "94%";

</script>

now I want to add to that input a childNode of label(password) and to that label another childNote for the password-field.

below is my try

<script>
  var label2 = document.createElement("label");
  label2.appendChild(document.createTextNode("password"));

  line_break.insertBefore(label2, line_break.children[1]).style.width = "94%";
  var input2 = document.createElement("input");

  label2.insertBefore(input2, label2.children[1]).style.width = "94%";
</script>
  • You should reorder your inserts to the document (or to any node). It is hard to read what has been inserted and what has not. – Seblor Feb 25 '19 at 15:04
  • Self-closing tags, like
    , cannot have content inside them. Also, have a look at CSS for all your layouting needs. Then you probably won't need
    tags anymore.
    – Shilly Feb 25 '19 at 15:11
  • alright @Shilly I edited my post. I want to add child to the first input –  Feb 25 '19 at 15:18

1 Answers1

1

You can't add a child to a BR element - only to its parent. Doing so, will automatically make it the last child, unless you specifically choose otherwise.

MikeB
  • 580
  • 3
  • 18
  • I seriously doubt it (I think that is also illegal syntax) - I think you want to add a sibling, which is done in almost exactly the same way you already did, just duplicate the first two lines of your existing code. – MikeB Feb 25 '19 at 15:53