J
john_williams_800
Hi;
I am just starting to use the DOM to do some more advanced
javascripting so please be patient with my question if it is an
ignorant question.
I would like to use the DOM to dynamically create an html table via
javascript. While that table is being dynamically created in a
javascript function I would like to dynamically insert text and a
hyperlink with an image into each cell.
I got pretty far on my own. I was able to dynamically create a table
and insert a string into each cell. However, I was not able to use
the DOM to insert a hyperlink into each cell. I tried adding it as a
new text node and got the text of a link rather then a link. I then
tried adding and setting an anchor element but I got nothing.
An example of how to add an image that is a link to a table cell using
the dom would be much appreciated.
Below is the code that I tried that did not work.
Any insights appreciated.
Thanks
//-------------------------------------------------------------------------------
// get a <div> off of the page, insert a 3x3 <table> into it with the
word
// "text" and a link to google in each cell
function doTable()
{
var d = document;
var table;
var tbody;
var row;
var cell;
var msg = "text";
var link;
var celltext;
var div;
// Get table off of page
div = d.getElementById("d1");
table = d.createElement("table");
tbody = d.createElement("tbody");
// 3 x 3 table
// Make a row
for (var r = 0; r < 3; r++) {
row = d.createElement("tr");
// make cells for row
for (var c = 0; c < 3; c++) {
cell = d.createElement("td");
// insert "text" into each cell
celltext = d.createTextNode(msg);
cell.appendChild(celltext);
// insert a link to google in each cell
link = d.createElement("a");
link.name = "google";
link.href = "http://www.google.com";
cell.appendChild(link);
// put cell into row
row.appendChild(cell);
}
tbody.appendChild(row);
}
table.appendChild(tbody);
table.setAttribute("border","2");
div.appendChild(table);
}// end function doTable()
//--------------------------------------------------------------------------
I am just starting to use the DOM to do some more advanced
javascripting so please be patient with my question if it is an
ignorant question.
I would like to use the DOM to dynamically create an html table via
javascript. While that table is being dynamically created in a
javascript function I would like to dynamically insert text and a
hyperlink with an image into each cell.
I got pretty far on my own. I was able to dynamically create a table
and insert a string into each cell. However, I was not able to use
the DOM to insert a hyperlink into each cell. I tried adding it as a
new text node and got the text of a link rather then a link. I then
tried adding and setting an anchor element but I got nothing.
An example of how to add an image that is a link to a table cell using
the dom would be much appreciated.
Below is the code that I tried that did not work.
Any insights appreciated.
Thanks
//-------------------------------------------------------------------------------
// get a <div> off of the page, insert a 3x3 <table> into it with the
word
// "text" and a link to google in each cell
function doTable()
{
var d = document;
var table;
var tbody;
var row;
var cell;
var msg = "text";
var link;
var celltext;
var div;
// Get table off of page
div = d.getElementById("d1");
table = d.createElement("table");
tbody = d.createElement("tbody");
// 3 x 3 table
// Make a row
for (var r = 0; r < 3; r++) {
row = d.createElement("tr");
// make cells for row
for (var c = 0; c < 3; c++) {
cell = d.createElement("td");
// insert "text" into each cell
celltext = d.createTextNode(msg);
cell.appendChild(celltext);
// insert a link to google in each cell
link = d.createElement("a");
link.name = "google";
link.href = "http://www.google.com";
cell.appendChild(link);
// put cell into row
row.appendChild(cell);
}
tbody.appendChild(row);
}
table.appendChild(tbody);
table.setAttribute("border","2");
div.appendChild(table);
}// end function doTable()
//--------------------------------------------------------------------------