javascriptappendcreateelementcreatetextnode

Create span with input value


I'm trying to get the value of an input, and create an element with the value text in it. I don't know why this isn't working.

(And, yes! I called the function. Just didn't post the jQuery enter key code.)

<input type="text" id="user-input"/>

 function submitUserMessage(){
    var message = document.getElementById("user-input");

    if(message.value){
        var userMessageElement = document.createElement("span");
        var userMessageElementText = userMessageElement.createTextNode(message.value);
        userMessageElement.appendChild(userMessageElementText);
        userMessageElement.className = "umsg";
        userMessageElement.style.backgroundColor = "#fff";
        document.body.appendChild(userMessageElement);
    }
}

Solution

  • you must call document.createTextNode() instead of userMessageElement.createTextNode()