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");
document.getElementById("myUL").innerHTML='<div>title<div>content</div></div>';document.getElementById("myUL").insertAdjacentHTML("beforeEnd", "<div>title<div>content</div></div>");