document.createElement('a') doesn't work?

W

Weston C

After noticing some problems with a script, I tried testing various bits
in the squarefree javascript shell (
http://www.squarefree.com/shell/shell.html ) and noticed this:

var anchor = document.createElement("a");
anchor
undefined

but:

var anchor = document.createElement("p");
anchor
[object HTMLParagraphElement]

Hmmmm. I tried it on a bunch of other elements, including some that
don't exist:

var anchor = document.createElement("rant");
anchor
[object HTMLUnknownElement]

So great. I can create a "rant" tag, but an anchor tag is right out?

Thanks,

Weston

~==~
http://weston.canncentral.org/
weston8[at]cann8central.org
(remove eights to email me)
 
J

Janwillem Borleffs

Weston C said:
After noticing some problems with a script, I tried testing various bits
in the squarefree javascript shell (
http://www.squarefree.com/shell/shell.html ) and noticed this:

var anchor = document.createElement("a");
anchor
undefined

Sounds like a bug in the squarefree program, try:
var anchor = document.createElement("a");
alert(typeof anchor);


JW
 
M

Martin Honnen

Weston said:
After noticing some problems with a script, I tried testing various bits
in the squarefree javascript shell (
http://www.squarefree.com/shell/shell.html ) and noticed this:

var anchor = document.createElement("a");
anchor
undefined

Are you sure you get undefined and not an empty string? The problem is
more likely that for most element objects in Mozilla toString() gives
something alike
[HTMLTagnameElement]
while historically toString() called on an <a> element yields the value
of the href property and obviously for
var anchor = document.createElement('a')
the href property is empty.
 
W

Weston C

Are you sure you get undefined and not an empty string? >The problem is
more likely that for most element
objects in Mozilla toString() gives something alike
[HTMLTagnameElement] while historically toString()
called on an <a> element yields the value
of the href property and obviously for
var anchor = document.createElement('a')
the href property is empty.

Exactly correct, apparently:

jsh> a = document.createElement('a')
undefined
jsh> a.href = 'localhost/index.html';
localhost/index.html
jsh> a
http://www.squarefree.com/shell/localhost/index.html

So it is indeed giving me back the href property on toString(), which is
not only resolves my current vexation, but seems like a useful piece of
knowledge to have. Gracias mucho!


~==~
http://weston.canncentral.org/
weston8[at]cann8central.org
(remove eights to email me)
 
M

Michael Hill

Here is how I create a new anchor that has an image tag:

//create the anchor node
myA=document.createElement("A");
url = "javascript:alert(" + "hi" + ");";
myA.setAttribute("href",url);
//create the image node
url = url_path + "images/del.gif";
myImg=document.createElement("IMG");
myImg.setAttribute("src",url);
myImg.setAttribute("width","16");
myImg.setAttribute("height","16");
myImg.setAttribute("border","0");
myImg.setAttribute("alt","Say Hi");
// Appends the image node to the anchor
myA.appendChild(myImg);

Hope that helps,

Mike
 

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,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top