473,388 Members | 1,606 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,388 software developers and data experts.

Question involving document.createElement and this

I'm definitely not new to JS, but for the life of me, I can't figure
this one out. Here's basically what I'm doing:

function foo() {
alert(this);
}

span = document.createElement('span');
a = document.createElement('a');
// Code to add text, etc to the tag here...
a.onclick = foo;
span.appendChild(a);
// ... stuff ...
divObject.appendChild(span);

Instead of seeing like [object HTMLElement] or whatever, I simply see
the URL of the page in the alert window. I've also tried adding (e) as
a parameter to foo to capture the event, but with no luck. And I tried
using an anonymous function:

a.onclick = function() { alert(this); }

I was thinking maybe it was just giving me the URL was because the a
object was not appended to anything, ergo belonging to the document,
but it seems like the 'this' reference shouldn't even matter until the
function is called, at which time it is set to the a object. Can anyone
help?

May 1 '06 #1
7 1538
Clarification:

I've also tried:

function foo(e) {
alert(e.target);
}
// [REST OF CODE]

and I get the exact same thing.

May 1 '06 #2
Update:

doing:

function foo(e) {
alert(e.parentNode.id);
}

correctly returns the id of the parent node of the a-link, which is the
span! Now I'm even more confused...

May 1 '06 #3
I had a similar situation, if I'm reading your right. See if this
helps any as a replacement to a.onclick.

a.href = "javascript:foo()";

May 1 '06 #4
ba***************@gmail.com writes:
function foo() {
alert(this);
} .... a = document.createElement('a');
// Code to add text, etc to the tag here...
a.onclick = foo; .... Instead of seeing like [object HTMLElement] or whatever, I simply see
the URL of the page in the alert window.
The URL of *which* page? The current or the one being linked to by the
link?

I believe you are seeing a legacy feature. The original browsers
introducing a method to refer to "a" elements (the document.links
collection) predates the W3C standards. They could do whatever
they wanted, and choose to have the toString method of "a" elements
return the href property. Modern browser have kept this feature.

And I tried using an anonymous function:

a.onclick = function() { alert(this); } .... but it seems like the 'this' reference shouldn't even matter until the
function is called, at which time it is set to the a object.


Correct.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
May 1 '06 #5
in**@hmdproducts.com said the following on 4/30/2006 11:54 PM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
<URL: http://www.safalra.com/special/googlegroupsreply/ >
I had a similar situation, if I'm reading your right. See if this
helps any as a replacement to a.onclick.

a.href = "javascript:foo()";


It won't.

<URL: http://jibbering.com/faq/#FAQ4_24>

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 1 '06 #6
ba***************@gmail.com wrote:
I'm definitely not new to JS,
I would say you are new to it enough.
but for the life of me, I can't figure
this one out. Here's basically what I'm doing:

function foo() {
alert(this);
}

span = document.createElement('span');
Identifiers should be declared:

var span = ...;

At least methods should be feature-tested:

function isMethod(a)
{
return a && /^\s*(function|object)\s*$/.test(typeof a);
}

if (isMethod(document.createElement))
{
... document.createElement(...) ...
}
a = document.createElement('a');
See above.
// Code to add text, etc to the tag here...
a.onclick = foo;
References should be tested before they are used:

<URL:http://pointedears.de/scripts/test/whatami#inference>
span.appendChild(a);
See above.
// ... stuff ...
divObject.appendChild(span);
See above.
Instead of seeing like [object HTMLElement] or whatever, I simply see
the URL of the page in the alert window.
The reason is that in many DOMs `HTMLAnchorElement' objects (and their Link
equivalents) inherit a toString() method from their prototype that returns
the value of their `href' property (with relative URIs resolved). Since
you have not set that property, it has its default value which is "" which
in turn is a relative URI for the resource where the element is located.
Which is, quite correctly, "the URL of the page", as window.alert()
converts its argument to string.
I've also tried adding (e) as
a parameter to foo to capture the event, but with no luck. And I tried
using an anonymous function:

a.onclick = function() { alert(this); }


As I expected, that does not make any difference. `this' refers to the
calling object (which is the event target with this syntax) and its `href'
property is still the empty string.

There are several possibilities, among them:

- You can use FireBug's Event Inspector
- You can access this.nodeName
- In the Gecko DOM, you can overwrite HTMLAnchorElement.prototype.toString()
(backing it up first) to show any data you find convenient, for example:

HTMLAnchorElement.prototype.toString = function()
{
return '<a href="' + this.href + '">';
}

See also <URL:http://pointedears.de/ObjectInspector?root=HTMLAnchorElement>.
PointedEars
--
Germany is the birth place of psychiatry. Psychs feel threatened by
Scientology as they are crazy. Many psychs become psychs to heal their own
mental problems and to control others. -- "The only real Barbara Schwarz",
dsw.scientology, <16**************************@posting.google.com >
May 10 '06 #7
Lasse Reichstein Nielsen wrote:
ba***************@gmail.com writes:
function foo() {
alert(this);
}

...
a = document.createElement('a');
// Code to add text, etc to the tag here...
a.onclick = foo;

...
Instead of seeing like [object HTMLElement] or whatever, I simply see
the URL of the page in the alert window.


The URL of *which* page? The current or the one being linked to by
the link?

I believe you are seeing a legacy feature. The original browsers
introducing a method to refer to "a" elements (the document.links
collection) predates the W3C standards. They could do whatever
they wanted, and choose to have the toString method of "a" elements
return the href property. Modern browser have kept this feature.


While doing research for <news:16****************@PointedEars.de>,
I have found out that this feature is in fact standards compliant:

<URL:http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html>
(scroll down to the bottom)
PointedEars
--
Let us not judge others because of their religion, color or nationality.
We are all just human beings living together on this planet. (poehoe.de)
May 16 '06 #8

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

Similar topics

4
by: HolaGoogle | last post by:
hi there, i've 2 questions for you guys.... 1: is there any way to "force" a session_onend(), session timeout or at least call my logout method when a user leaves the application window without...
8
by: Sergio Otoya | last post by:
Hi all, I need to add an input hidden field to an existing form (post). I have tried a couple things like adding the '<INPUT type=hidden name=idSelectedURL value=http://server/documents>' to...
6
by: skubik | last post by:
Hi everyone. I'm attempting to write a Javascript that will create a form within a brand-new document in a specific frame of a frameset. The problem is that I can create the form and input...
2
by: joltman | last post by:
OK, this is kind of hard to explain, so I'll do my best: I have a form where I have a row where there could be multiple entries, so I have a link where it will dynamically add another row like it,...
3
by: King Albert | last post by:
Why does the 'insertrow' work in this situation: <html> <script type="text/javascript"> function addRow(tableID) { var tableRef = document.getElementById(tableID); var newRow =...
1
by: eggie5 | last post by:
I have the following function that build a div and adds it to the page. function setDataJSON(req) { var data = eval('(' + req.responseText + ')'); console.log(data.news.length); for (var...
3
by: Tarik Monem | last post by:
You guys have been very helpful. Even if the solution was not presented in the reply, then you would at least spark a thought which would lead to the solution and I am very grateful for your...
7
by: Tarik Monem | last post by:
Why am I having so much trouble with using DOM in IE & Opera to create, then remove an Object & Embedded element? Here's the code that works in Firefox (Mac/Win/Linux) and Safari, but not on IE or...
25
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.