javascript - How to add Elements to DOM - Stack Overflow

I need a function which creates element and than adds text to that element and than adds that new eleme

I need a function which creates element and than adds text to that element and than adds that new element to a some location in DOM. I am noob to this. I find this function but I don't know how to automaticaly specify location so that I can just call function and for example specify third argument as an element to which I want to append new element.

function appendElement (elemNode,textNode) {
            var element = document.createElement(elemNode);
            var text = document.createTextNode(textNode);
            element.appendChild(text);
            document.body.appendChild(element);
}
appendElement("b","lorem");

I need a function which creates element and than adds text to that element and than adds that new element to a some location in DOM. I am noob to this. I find this function but I don't know how to automaticaly specify location so that I can just call function and for example specify third argument as an element to which I want to append new element.

function appendElement (elemNode,textNode) {
            var element = document.createElement(elemNode);
            var text = document.createTextNode(textNode);
            element.appendChild(text);
            document.body.appendChild(element);
}
appendElement("b","lorem");
Share Improve this question asked Sep 22, 2014 at 6:41 MikeMike 511 silver badge4 bronze badges 3
  • 2 Change document.body to the container where you want to append it. – elclanrs Commented Sep 22, 2014 at 6:41
  • 1 developer.mozilla/en-US/docs/Web/API/Node.appendChild – MarsOne Commented Sep 22, 2014 at 6:43
  • yes, but I need this on a few locations. How can I specify location where I want to add element. Something like this appendElement("b","lorem","body"); appendElement("b","lorem","div"); appendElement("b","lorem","span"); – Mike Commented Sep 22, 2014 at 6:45
Add a ment  | 

4 Answers 4

Reset to default 2
function appendElement (elemNode,textNode,containerToAppend) {
        var container = document.getElementById(containerToAppend);
        var element = document.createElement(elemNode);
        var text = document.createTextNode(textNode);
        element.appendChild(text);
        container.appendChild(element);
}
appendElement("b","lorem","ContainerId");

Here's a way to do it in one line:

function appendElement (elemNode, textContent, parent) {
    parent.appendChild(document.createElement(elemNode)).textContent = textContent;
}

appendElement("b","lorem", document.getElementById("container"));
div { background-color: aqua }
<div id="container"></div>

function appendElement (elemNode,textNode,containerToAppend) {
    var container = document.getElementById(containerToAppend);
    var element = document.createElement(elemNode);
    var text = document.createTextNode(textNode);
    element.appendChild(text);
    container.appendChild(element);
}
appendElement("p","this is text","ContainerId");

Here is my attempt. Completely ripped off from developer.Mozilla with a minor tweak.

JSFIDDLE DEMO

Javascript:

function addElement() {
    // Create a new div element  and give it some content 
    var newDiv = document.createElement("div");
    var newContent = document.createTextNode("Hi there and greetings!");
    newDiv.appendChild(newContent); //Add the text node to the newly created div. 

    // Add the newly created element and its content into the Parent of the clicked button
    var parentDiv = event.target.parentNode;
    document.body.insertBefore(newDiv, parentDiv);
}

HTML

<div id="div1">
    <input type="button" onclick="addElement(event)" value="Click me to add div" />
</div>
<div id="div2">
    <input type="button" onclick="addElement(event)" value="Click me to add div" />
</div>
<div id="div3">
    <input type="button" onclick="addElement(event)" value="Click me to add div" />
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744655436a4586151.html

相关推荐

  • javascript - How to add Elements to DOM - Stack Overflow

    I need a function which creates element and than adds text to that element and than adds that new eleme

    3天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信