-2

How can i add an createElement(br) before and after the table? I tried it with doc.body.appendChild(p); at the end, but nothing happens. i don't know where i have to write the appendchild

function insertBlock(border) 
{       
    var doc = document.getElementById("frame").contentWindow.document;
    var p = doc.createElement("br");            

    var range = doc.getSelection().getRangeAt(0);
    myParent=document.getElementById("frame").contentWindow.document.body;
    if (border == true)
    {
        myTable=document.createElement("table");
        myTable.setAttribute("style","border: 1px solid #000000;");
    }
    else
    {
        myTable=document.createElement("table");
    }
    // IE5, IE6 benoetigen unbedingt tbody-Element
    myTBody=document.createElement("tbody");
    myRow=document.createElement("tr");

    myCell=document.createElement("td");

    myText=document.createTextNode("Die erste Zelle");
    myCell.appendChild(myText);
    myRow.appendChild(myCell);

    myCell=document.createElement("td");
    myText=document.createTextNode("Die zweite Zelle");
    myCell.appendChild(myText);
    myRow.appendChild(myCell);
    myTBody.appendChild(myRow);
    myTable.appendChild(myTBody);
    myParent.appendChild(myTable);

    range.insertNode(myTable);

}   
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
stefan
  • 55
  • 5
  • You mean [like this](http://jsfiddle.net/amullins/Laznwndc/1/)? – Austin Mullins Apr 10 '15 at 21:34
  • 1
    If you're doing this for spacing out elements then you're definitely taking the wrong approach. You should look at adding margin with CSS to achieve this. `
    `'s should be a thing of the past. Also, with this amount of DOM manipulation, I would also try looking at a templating library like Handlebars. http://handlebarsjs.com/
    – arjabbar Apr 10 '15 at 21:35
  • That said, if you look at the comments in the code, they're targeting IE5, which is itself a thing of the past. – Austin Mullins Apr 10 '15 at 21:36

2 Answers2

1

Should be here:

myParent.appendChild(p);
myParent.appendChild(myTable);
myParent.appendChild(p.cloneNode());

Notice that the second one is a clone. When it's not. Only the second one will be added. (You can not add an element twice).

You can also add two different objects of course.

myParent.appendChild(p);
myParent.appendChild(myTable);
myParent.appendChild(p2);

Or withoud variables:

myParent.appendChild(doc.createElement("br"));
myParent.appendChild(myTable);
myParent.appendChild(doc.createElement("br"));
Dennis van Dalen
  • 589
  • 4
  • 13
  • I tried on the beginnig of myParent and at the end. But the two br will add at the end, why? myParent=document.getElementById("frame").contentWindow.document.body; myParent.appendChild(p2); And at the end the same myParent.appendChild(p); – stefan Apr 10 '15 at 22:32
  • You do append a
    before appending the table? Have you checked that p and p2 are actually set?
    – Dennis van Dalen Apr 11 '15 at 08:10
  • Do you have it somewhere online? – Dennis van Dalen Apr 12 '15 at 15:29
0

Try this:

myTable.insertAdjacentHTML('afterend','<br />');
myTable.insertAdjacentHTML('beforebegin','<br />');

if you want to do it using createElement('br').

var p = doc.createElement("br");            
myParent.insertBefore(p,myTable);
myParent.insertBefore(p.cloneNode(),myTable.nextSibling);
Omar Elawady
  • 3,300
  • 1
  • 13
  • 17