473,378 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Using the dom to dynamically build a table with hyperlinks

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()
//--------------------------------------------------------------------------

Jul 23 '05 #1
5 4870
This modification should work:

cell = d.createElement("td");

// insert "text" into each cell
celltext = d.createTextNode(msg);

// insert a link to google in each cell
link = d.createElement("a");
link.name = "google";
link.href = "http://www.google.com";
link.appendChild(celltext); // <---
cell.appendChild(link);

// put cell into row
row.appendChild(cell);

Regards,
Elias
<jo***************@hotmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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

Jul 23 '05 #2
Hi Elias;

That helped get me much closer to where I want to go, but I am still
not there.

What I want to do is create a table with 3 rows and 3 cells.

Each cell has a text message with a hyperlink to the right of the
message. The hyperlink is labeled with an image instead of text;

myStringInTheCellBeforeALink myLinkWithAnImage

Using your example I got the link with the image into each cell ( yay!
), but I can't seem to put a text message to the left of it.

Below is the code I am using.

Any help or examples would be appreciated.

Thanks

Steve

//-------------------------------------------------------------------------------
// get a <div> off of the page, insert a 3x3 <table> into it. Each
cell has
// a string followed by a hyperlink that uses an image
function doTable()
{
var d = document;
var table;
var tbody;
var row;
var cell;
var link;
var div;
var imgForLink;

// 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");

imgForLink = d.createElement("img");
imgForLink.src = "rc.jpg";

// insert a link to google in each cell
link = d.createElement("a");
link.name = "google";
link.href = "http://www.google.com";

//Label the link with an image
link.appendChild(imgForLink);

// Put a text message to the left of the image
link.insertAdjacentText("BeforeBegin", "text string before
link");

// Shove it all into a cell
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()

Jul 23 '05 #3


jo***************@hotmail.com wrote:

//Label the link with an image
link.appendChild(imgForLink);

// Put a text message to the left of the image
link.insertAdjacentText("BeforeBegin", "text string before
link");


You need to insert the text first e.g.
link.appendChild(document.createTextNode("text goes here"));
then insert the image e.g.
link.appendChild(imgForLink);
That way the link has two child nodes, a text node and the <img> element
node.

Or perhaps you want the cell to contain two child nodes, a text node and
the link with the image element node, then you need
cell.appendChild(document.createTextNode("text goes here"));
cell.appendChild(link);

Other approaches are possible but if you build the whole stuff from
scratch then it is usually easiest to simply use appendChild calls in
the order you want to have the child nodes.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #4
Thats it!

Martin, thanks so much for your help.

You mentioned that there would be a better way if I would do it from
scratch.

How would you do it?

This is all new to me so I wouldn't mind having a look at a better way.

Thanks again!

Jul 23 '05 #5


jo***************@hotmail.com wrote:

You mentioned that there would be a better way if I would do it from
scratch.


No, what I suggested with appendChild is in my view the easiest way if
you build the whole DOM from scratch. But the DOM has other methods like
insertBefore which could help to insert nodes at a certain position
between or before other nodes.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tim | last post by:
Access 97: I have a table with a hyperlink field that I display on a form. I can click on the form field and the hyperlink activates correctly. However, if I try to activate the hyperlink using VB...
2
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
1
by: Yannis CORRE via .NET 247 | last post by:
Hello, I have two questions : 1 - I want to generate dynamically a personnalized table from a dataset. I want to create each of my rows and columns. So, I use a HtmlTable and I build my Table...
1
by: Brett | last post by:
Hi, Here's a strange one: I have built a single page model for a website, that uses 3 panel controls which load usercontrols dynamically, based on the querystring PageMode parameter that you...
3
by: Joel Barsotti | last post by:
What I mean here is that I have to build an HTML interface for a CD catalouge. All the contents for this catalouge are in a DB right now for the current ..net website. Since the CD catalouge...
1
by: vj | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file? I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
0
by: vijendra | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file?I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
1
by: mforema | last post by:
Hi Everybody, I have an Access table with a field filled with outdated hyperlinks. I also have an excel spreadsheet that is the master list for these hyperlinks. The spreadsheet will be updated...
3
by: N L | last post by:
Greetings, I want to create a form in Access that shows a list of checkboxes to a user. The checkboxes will be dynamically generated, showing the fields in a table the user has selected in a...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.