Web Analytics Made Easy -
StatCounter add images to table's cell - CodingForum

Announcement

Collapse
No announcement yet.

add images to table's cell

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • add images to table's cell

    Hi,

    How can I add images in to cell? The pictures would come only to two most left columns (red.svg) and to two most right columns (blue.svg) as in chess. Under is my code .

    function createTable(hight, width) {

    var table = document.getElementById("table");

    if (document.getElementById("tbody")) {
    var temp = table.removeChild(document.getElementById("tbody"));
    }

    var tbody = document.createElement("tbody");
    tbody.setAttribute("id", "tbody");

    for (var i = 0; i < hight; i++) {
    var row = document.createElement("tr");

    for (var j = 0; j < width; j++) {
    var cell = document.createElement("td");
    row.appendChild(cell);

    var img1 = document.getElementsByTagName('img');
    var img2 = document.getElementsByTagName('img');
    img1.src = 'blue.SVG';
    img2.src = 'red.SVG';
    cell.appendChild(img1);
    cell.appendChild(img2);
    }
    tbody.appendChild(row);
    }
    table.appendChild(tbody);
    }

  • #2
    If you are removing the entire tbody then you no longer have any content in the table.

    use insertRow() and insertCell() to add the <tr> and <td> tags rather than createElement()
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

    Comment

    Working...
    X