Href Links in Dynamic table

W

webmaniac

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.
 
E

Erwin Moller

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
 
W

webmaniac

webmaniac schreef:









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.
 
G

Gregor Kofler

webmaniac meinte:
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
 
E

Erwin Moller

webmaniac schreef:
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/12/29/quick-answer-dynamically-adding-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 <a> text node to the <a> element
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
 
W

webmaniac

webmaniac schreef:





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/12/29/quick-answer-dynamically-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 <a> text node to the <a> element
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top