Tables and anchors

H

Hooligan

Hi

I'm attempting to add a hyperlink to a table cell using javascript.

Here's the code:

var cell = document.getElementById("td1");
var aHref = document.createElement("A");
aHref.innerText = "hello";
aHref.href = "index.htm";
aHref.title = "hello";
aHref.id= "alinkage";
cell.appendChild(aHref);

This works with IE but I can't get it to work with Firefox.

I can see that cell.childNodes is incremented after running this code
but nothing is displayed.

Any tips would be grateful.
 
M

Michael Winter

[snip]
var aHref = document.createElement("A");
aHref.innerText = "hello";
[snip]

This works with IE but I can't get it to work with Firefox.

That's because innerText is a proprietary property invented by Microsoft,
and most user agents don't support it.

[snip]
Any tips would be grateful.

var text = document.createTextNode('hello');
aHref.appendChild(text);

Mike
 
M

Martin Honnen

Hooligan wrote:

var aHref = document.createElement("A");
aHref.innerText = "hello";

You need to create text node and append that e.g.
aHref.appendChild(document.createTextNode("hello"));
as innerText is not part of the W3C DOM and is not implemented by
Mozilla. However creating and appening text nodes works as well in IE so
there is no need to use innerText above.
 
H

Hooligan

Martin Honnen said:
Hooligan wrote:



You need to create text node and append that e.g.
aHref.appendChild(document.createTextNode("hello"));
as innerText is not part of the W3C DOM and is not implemented by
Mozilla. However creating and appening text nodes works as well in IE so
there is no need to use innerText above.

Thanks for the prompt responses though I solved it when I used
innerHTML (I'd tried earlier with this and failed because of a typo).

createElement is working and is documented at W3C and appears more
appropriate for my needs than createTextNode.
 
D

DU

Hooligan said:
Thanks for the prompt responses though I solved it when I used
innerHTML (I'd tried earlier with this and failed because of a typo).

createElement is working and is documented at W3C and appears more
appropriate for my needs than createTextNode.

but "hello" is a string, not an HTML element. What Martin replied to you
is correct, best cross-browser and web standards compliant method of
creating a text node.

DU
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top