4

Is there any predefined method in javascript that can append div after a div? For example:

<div class="uploader">
    <div class="file-metas">
        <div class="file-name">status<span class="file-size">1kb</span></div>
        <p class="state state-success">Success</p>
    </div>
</div>

Now I want to insert another div with class name 'remove' after 'uploader' div.

5
  • 1
    Maybe you should indent your HTML code so we can read it. :-) Commented Jun 5, 2013 at 9:38
  • you can use jquery to make your task easier Commented Jun 5, 2013 at 9:40
  • @rajansoft1: I m not using jquery, so i want only javascript code Commented Jun 5, 2013 at 9:41
  • So like this one? stackoverflow.com/questions/7690582/… Commented Jun 5, 2013 at 9:46
  • @Anjil panchal: any reason you are not using jquery? just wanted to know why you are not willing to use jquery. Commented Jun 5, 2013 at 10:11

5 Answers 5

7

Vanilla JS: Supported with all the browsers:

Visualization of position names

<!-- beforebegin -->
<p>
  <!-- afterbegin -->
  foo
  <!-- beforeend -->
</p>
<!-- afterend -->

code

// <div id="one">one</div>
var d1 = document.getElementById('one');
d1.insertAdjacentHTML('afterend', '<div id="two">two</div>');

// At this point, the new structure is:
// <div id="one">one</div><div id="two">two</div>

More info here

Sign up to request clarification or add additional context in comments.

Comments

5

Yeah it is possible using pure javascript

You can use insertBefore method to do so by accessing parent node of target element.

document.getElementsByClassName("uploader").parentNode

Take a look

Comments

5

Try this Demo

var node = document.querySelector(".uploader"),
    ele = document.createElement("div");

ele.className = "remove";
ele.innerHTML = "some text";
node.parentNode.insertBefore(ele, node.nextSibling);

Comments

0
function appendAfter(divToAppend, siblingBefore) {
    if(siblingBefore.nextSibling) {
        siblingBefore.parentNode.insertBefore(divToAppend, siblingBefore.nextSibling);
    } else {
        siblingBefore.parentNode.appendChild(divToAppend);
    }
}

Comments

0

The following example demonstrates what you are trying to achieve using plain JavaScript:

<html>
<head>

<script>

function addNewDiv() {
    var newDiv = document.createElement('div');
    var label = document.createTextNode(" - new div - ");
    var elements = document.getElementsByClassName('uploader');
    var uploaderDiv = elements[0];

    newDiv.appendChild(label);
    elements[0].insertBefore(newDiv, uploaderDiv.children[0]);
}    

</script>

</head>
<body>

<div class="uploader">
    <div class="file-metas">
        <div class="file-name">status<span class="file-size">1kb</span>
        </div>
        <p class="state state-success">Success</p>
    </div>
</div>

<input type="button" onClick="addNewDiv()" value="Add new Div" />

</body>
</html>

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.