By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,259 Members | 1,708 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,259 IT Pros & Developers. It's quick & easy.

Href Links in Dynamic table

P: n/a
Hi,

I am creating a table dynamically in javascript.

All is fine but when I try to put any ahref link on the cell its not
taking it. I am doing like that.
var rowHead1 = document.createElement('TR');
var CellHead1 = document.createElement('TD');
var text1 = "<a href=http://www.google.com>" + someVariable + "</a>
var cellHeadText1 = document.createTextNode(text1);
CellHead1.appendChild(cellHeadText1);

But somehow when I run this example.
It shows like that in Table cell.
<a href=http://www.google.com>abc</a>

Its not converting it into a Link.

Any Idea, what's going wrong here.
Nov 12 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
webmaniac schreef:
Hi,

I am creating a table dynamically in javascript.

All is fine but when I try to put any ahref link on the cell its not
taking it. I am doing like that.
var rowHead1 = document.createElement('TR');
var CellHead1 = document.createElement('TD');
var text1 = "<a href=http://www.google.com>" + someVariable + "</a>
var cellHeadText1 = document.createTextNode(text1);
CellHead1.appendChild(cellHeadText1);

But somehow when I run this example.
It shows like that in Table cell.
<a href=http://www.google.com>abc</a>

Its not converting it into a Link.

Any Idea, what's going wrong here.
Hi,

Not sure, but shouldn't you at least quote the hyperlink properly?
Like this:
var text1 = "<a href='http://www.google.com'>" + someVariable + "</a>";

Regards,
Erwin Moller
--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
Nov 12 '08 #2

P: n/a
On Nov 12, 12:40 pm, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
webmaniac schreef:
Hi,
I am creating a table dynamically in javascript.
All is fine but when I try to put any ahref link on the cell its not
taking it. I am doing like that.
var rowHead1 = document.createElement('TR');
var CellHead1 = document.createElement('TD');
var text1 = "<a href=http://www.google.com>" + someVariable + "</a>
var cellHeadText1 = document.createTextNode(text1);
CellHead1.appendChild(cellHeadText1);
But somehow when I run this example.
It shows like that in Table cell.
<a href=http://www.google.com>abc</a>
Its not converting it into a Link.
Any Idea, what's going wrong here.

Hi,

Not sure, but shouldn't you at least quote the hyperlink properly?
Like this:
var text1 = "<a href='http://www.google.com'>" + someVariable + "</a>";

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
I tried that too, but it still dont work.
I dont know, where I am getting wrong.
Nov 12 '08 #3

P: n/a
webmaniac meinte:
>>var text1 = "<a href=http://www.google.com>" + someVariable + "</a>
var cellHeadText1 = document.createTextNode(text1);
I tried that too, but it still dont work.
I dont know, where I am getting wrong.
Why should it? Anchors are not text nodes. Something like

var a = document.createElement("a");
a.href = "...";
a.appendChild(document.createTextNode("whatever");
CellHead1.appendChild(a);

Your "approach" requires to fill the (non-standard) innerHTML property
of CellHead1.

Gregor
Nov 12 '08 #4

P: n/a
webmaniac schreef:
On Nov 12, 12:40 pm, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
>webmaniac schreef:
>>Hi,
I am creating a table dynamically in javascript.
All is fine but when I try to put any ahref link on the cell its not
taking it. I am doing like that.
var rowHead1 = document.createElement('TR');
var CellHead1 = document.createElement('TD');
var text1 = "<a href=http://www.google.com>" + someVariable + "</a>
var cellHeadText1 = document.createTextNode(text1);
CellHead1.appendChild(cellHeadText1);
But somehow when I run this example.
It shows like that in Table cell.
<a href=http://www.google.com>abc</a>
Its not converting it into a Link.
Any Idea, what's going wrong here.
Hi,

Not sure, but shouldn't you at least quote the hyperlink properly?
Like this:
var text1 = "<a href='http://www.google.com'>" + someVariable + "</a>";

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare

I tried that too, but it still dont work.
I dont know, where I am getting wrong.
Ahum, sorry.

You know this feeling you are knew how something worked but totally
cannot retieve it?
I just had that great experience again, but a little googling helped.

You need to create the hyperlink element first ('a') and append that.
this is the trick:
var aElem = document.createElement('a');

Here is a sample from:
http://acsummer.wordpress.com/2007/1...g-a-hyperlink/

function appendLink()
{
//Get the element that we want to append to
var divEl = document.getElementById('link_div');
//Create the new <a>
var aElem = document.createElement('a');
aElem.href="http://www.google.com";
//Create a text node to hold the text of the <a>
var aElemTN = document.createTextNode('link to Google.com');
//Append the <atext node to the <aelement
aElem.appendChild(aElemTN);
//Append the new link to the existing <div>
divEl.appendChild(aElem);
}
That should help. :P

Regards,
Erwin Moller
--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
Nov 12 '08 #5

P: n/a
On Nov 12, 2:16 pm, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
webmaniac schreef:
On Nov 12, 12:40 pm, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
webmaniac schreef:
>Hi,
I am creating a table dynamically in javascript.
All is fine but when I try to put any ahref link on the cell its not
taking it. I am doing like that.
var rowHead1 = document.createElement('TR');
var CellHead1 = document.createElement('TD');
var text1 = "<a href=http://www.google.com>" + someVariable + "</a>
var cellHeadText1 = document.createTextNode(text1);
CellHead1.appendChild(cellHeadText1);
But somehow when I run this example.
It shows like that in Table cell.
<a href=http://www.google.com>abc</a>
Its not converting it into a Link.
Any Idea, what's going wrong here.
Hi,
Not sure, but shouldn't you at least quote the hyperlink properly?
Like this:
var text1 = "<a href='http://www.google.com'>" + someVariable + "</a>";
Regards,
Erwin Moller
--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
I tried that too, but it still dont work.
I dont know, where I am getting wrong.

Ahum, sorry.

You know this feeling you are knew how something worked but totally
cannot retieve it?
I just had that great experience again, but a little googling helped.

You need to create the hyperlink element first ('a') and append that.
this is the trick:
var aElem = document.createElement('a');

Here is a sample from:http://acsummer.wordpress.com/2007/1...ynamically-add...

function appendLink()
{
//Get the element that we want to append to
var divEl = document.getElementById('link_div');
//Create the new <a>
var aElem = document.createElement('a');
aElem.href="http://www.google.com";
//Create a text node to hold the text of the <a>
var aElemTN = document.createTextNode('link to Google.com');
//Append the <atext node to the <aelement
aElem.appendChild(aElemTN);
//Append the new link to the existing <div>
divEl.appendChild(aElem);
}

That should help. :P

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
Thanks Everyone, It worked.
Nov 12 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.