How to insert an element between two elements?

Asked

Viewed 4,195 times

4

Explanation:

I have a <div> parent containing two <div> daughters, however I would like to insert a <table> using javascript, between, that is, in the middle of these two <div> daughters who already exist in <div> father.

But mine <table> is a string that contains the HTML code as value.

Code of my <table> string:

var HTMLString = '<table id="teste"><tbody id="appender"><tr class="header"><td class="cod">Código</td> <td class="nome">Nome</td><td class="tipo">Cidade - Estado</td></tr></tbody></table>';

HTML of father and daughter div:

<div id=elementoPai>
  <div id="fixedHeader_outer">
      <div id="fixedHeader">conteudo</div>
  </div>
<!-- quero que a HTMLString se transforme num html que apareça aqui no meio das duas -->
  <div id="pnlOpcoes_outer">
    <div id="pnlOpcoes">conteudo</div>
  </div>
</div>

Example in Jsfiddle

Question:

How could I do this? I would like several solutions, citing the advantages and disadvantages of them, being able to use jQuery or not

  • Is it jQuery tag... is it or is it pure JS?

  • can be in jQuery too, but it is necessary ? is faster ? what is the usefulness of using jQuery in this context? is at your discretion but I accept

  • One advantage of jQuery, considering the answers, is a code line for four with pure JS.

  • @Pauloroberto, Javascript is always faster than jQuery, however it can be simpler to start working on different browsers with jQuery

  • jQuery is javascript, not another language. It’s so ugly that I’ve seen browser with jQuery pre-loaded, to get out faster.

3 answers

9

In pure Javascript you can do so:

var HTMLString = '<table id="teste"><tbody id="appender"><tr class="header"><td class="cod">Código</td> <td class="nome">Nome</td><td class="tipo">Cidade - Estado</td></tr></tbody></table>';

var ref = document.getElementById('pnlOpcoes_outer');
var div = document.createElement('div');
div.innerHTML = HTMLString;
ref.parentElement.insertBefore(div.firstChild, ref);

Demo no jsfiddle

This works in this case because your HTMLString only contains a loose element at the root. A more generic option:

var ref = document.getElementById('pnlOpcoes_outer');
var div = document.createElement('div');
div.innerHTML = HTMLString;
while(div.firstChild) {
    ref.parentElement.insertBefore(div.firstChild, ref);
}

Demo no jsfiddle

It also has a dark option, but compatible even with very old browsers, using Element.insertAdjacentHTML (tip from Emerson Rocha Luiz):

var ref = document.getElementById('pnlOpcoes_outer');
ref.insertAdjacentHTML("beforebegin", HTMLString);

Demo no jsfiddle

4

Take the first div and insert after her:

$('#fixedHeader_outer').after(HTMLString);

I want to participate in the codegolf: if you do not want to use jQuery there is this option, as short as the original:

fixedHeader_outer.outerHTML += HTMLString;

As requested, the compatibility table.

  • Won by 7 seconds...

  • Don’t feel dead @utluiz I’m anxious to see your answer =p

  • @Pauloroberto I’m referring to Miguel... I think in this case I have nothing to add, considering the reply of bfavaretto

  • ah yes, now I understand. :)

  • 1

    The answer is the same: this is very trivial. Like, very.

  • Doubt: already has three answers, by the tag OP asked jQuery but everyone wants to innovate. I edit my answer to participate in this codegolf? Nothing as simple as fixedHeader_outer.outerHTML += HTMLString;

  • yes for sure @Gustavorodrigues I just want is this, several solutions, later I will put all in jsperf and see which the fastest =p

  • Could edit the answer with method compatibility using .outerHTML? would like to know if this method is compatible with all browsers currently used

Show 3 more comments

3

By the following tags you are using jQuery. So here is the solution:

$('#fixedHeader_outer').after(HTMLString);

If you’re not using jQuery, it would look like this:

var referenceNode = document.getElementById('fixedHeader_outer');
var newNode = document.createElement('table');
newNode.innerHTML = '<tbody id="appender"><tr class="header"><td class="cod">Código</td> <td class="nome">Nome</td><td class="tipo">Cidade - Estado</td></tr></tbody>';

referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.