2

I've been looking for a way to create a div with a div inside to create a box with a header with javascript.

ie.

<div>title<div>content</div></div>

The code I have

function addpost() {
  var div = document.createElement("div");
  div.id = "item";
  var t = document.createTextNode("title<div>content</div>");
  div.appendChild(t);
  if (inputValue === '') {
    alert("cannot be empty");
  } else {
   document.getElementById("myUL").appendChild(div);
  }
}

The result that I have been getting ends up in the webpage. :(

title<div>content</div>
7
  • 1
    document.getElementById("myUL").innerHTML='<div>title<div>content</div></div>'; Commented Apr 24, 2017 at 9:23
  • just use .innerHTML = "your html" and insert in a div Commented Apr 24, 2017 at 9:24
  • Trying to make it dynamic. That would just erase my previous content right? I would like it to add on. Commented Apr 24, 2017 at 9:25
  • 2
    document.getElementById("myUL").insertAdjacentHTML("beforeEnd", "<div>title<div>content</div></div>"); Commented Apr 24, 2017 at 9:30
  • I think I found what was wrong, let me know if my answer worked for you :) Commented Apr 24, 2017 at 9:38

3 Answers 3

1

Try this

function addpost() {
  var div = document.createElement("div");
  div.id = "item";
  var t = document.createTextNode("title");
  div.appendChild(t);
  var x = document.createElement("div");
  var y = document.createTextNode("content");
  x.appendChild(y);
  div.appendChild(x);
  document.getElementById("myUL").appendChild(div);
}
Sign up to request clarification or add additional context in comments.

Comments

1

1st Problem:

You forgot to include inputValue as a parameter in the function. You manually included the text to include with this line: var t = document.createTextNode("title<div>content</div>"); when you should have done var t = document.createTextNode(inputValue);

This code should work for you:

function addpost(inputValue) {
  var div = document.createElement("div");
  div.id = "item";
  var t = document.createTextNode(inputValue);
  div.appendChild(t);
  if (inputValue === '') {
    alert("cannot be empty");
  } else {
   document.getElementById("myUL").appendChild(div);
  }
}
addpost("title<div>content</div>");

2nd Problem

You are inserting "title<div>content</div>" as text. This means the tags will not be considered as code, but will be escaped. To fix this, what you make var t another node, and include the text within var t.

This code should work for you:

   function addpost(titleInput, inputValue) {
      var div = document.createElement("div");
      div.id = "item";
      var t = document.createElement("div");
      t.id = "text";
      var t2 = document.createTextNode(inputValue);
      t.appendChild(t2);
      div.appendChild(document.createTextNode(titleInput));
      div.appendChild(t);
      if (inputValue === '') {
        alert("cannot be empty");
      } else {
       document.getElementById("myUL").appendChild(div);
      }
    }
    addpost("title","content");

1 Comment

thanks for the help, however Baraka posted the first working response and the second way you submitted works. However, the first one didn't work :(. upvoted though since it was correct.
0

For adding the content dynamically without deleting the content that was set there before, you also add it like this:

 div.innerElement = div.innerElement + "here comes the new stuff";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.