function insertNodeAtCursor(node) { var range, html; if (window.getSelection && window.getSelection().getRangeAt) { range = window.getSelection().getRangeAt(0); range.insertNode(node); } else if (document.selection && document.selection.createRange) { range = document.selection.createRange(); html = (node.nodeType == 3) ? node.data : node.outerHTML; range.pasteHTML(html); } } function insertTable(rows, columns, tableWidthPercentage, borderWidth, redColor, greenColor, blueColor) { // get the reference for the body -> div //-END->var bodyDiv = document.getElementsByTagName("div")[0]; // add
to body -> div center = document.createElement("center"); center.setAttribute("contenteditable", true); //-END->bodyDiv.appendChild(center); insertNodeAtCursor(center); // creates a element and a element var tbl = document.createElement("table"); var tblBody = document.createElement("tbody"); // creating all cells for (var j = 0; j < rows; j++) { // creates a table row var row = document.createElement("tr"); for (var i = 0; i < columns; i++) { // Create a in the
element and a text node, make the text // node the contents of the , and put the at // the end of the table row var cell = document.createElement("td"); var cellText = document.createTextNode("("+j+","+i+")"); cell.appendChild(cellText); row.appendChild(cell); } // add the row to the end of the table body tblBody.appendChild(row); } // put the
tbl.appendChild(tblBody); // appends
into body -> div -> center center.appendChild(tbl); // sets the table attributes tbl.setAttribute("border", borderWidth); tbl.setAttribute("cellspacing", "0"); tbl.setAttribute("cellpadding", "3"); tbl.setAttribute("width", ""+tableWidthPercentage+"%"); tbl.setAttribute("contenteditable", true); // sets the table background color //tbl.setAttribute("bgcolor", "rgb("+redColor+","+greenColor+","+blueColor+")"); //tbl.setAttribute("bgcolor", "#FF0000"); redColorSTR = redColor.toString(16); greenColorSTR = greenColor.toString(16); blueColorSTR = blueColor.toString(16); if(redColorSTR.length==1) redColorSTR = "0"+redColorSTR; if(greenColorSTR.length==1) greenColorSTR = "0"+greenColorSTR; if(blueColorSTR.length==1) blueColorSTR = "0"+blueColorSTR; tbl.setAttribute("bgcolor", "#"+redColorSTR+greenColorSTR+blueColorSTR+""); // insert new blank lines after the table par = document.createElement("p"); par.setAttribute("contenteditable", true); //-END->bodyDiv.appendChild(par); insertNodeAtCursor(par); br = document.createElement("br"); par.appendChild(br); }