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

Onclick and DOM problem

P: n/a
Hi every body,

I have a little pb and I'm turning around :

function MyFCTN(var1,var2) {
var mytable = document.getElementById("myTBL");
for (var i=myTBL.childNodes.length-1; i>0; i--){
myTBL.removeChild(myTBL.childNodes[i]);
}
for(var i=0; i<var1.length; i++){
var tr = document.createElement('TR');
var td1 = document.createElement('TD');
var a1 = document.createElement('A');
a1.appendChild(document.createTextNode('This is the link'));
a1.setAttribute=("HREF","#");
a1.onclick=function(){alert(var1[i].value)};
td1.appendChild(a1);
tr.appendChild(td1);
}
}

Error: var1[...] is null or not an object

And if put this :
a1.setAttribute=("onclick","var1[i].value");
this show me the result but on load and not onclick !!!

Help!
Thanks

Fred

May 29 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Fr*********@gmail.com wrote:
Hi every body,
Maybe you should have addressed the minds as well ...
I have a little pb and I'm turning around :
What is a `pb'? Can't you write properly?
function MyFCTN(var1,var2) { [1]^^^^^^ ^^^^[2]

[1]
I was always of the opinion that identifiers should convey the least bit of
meaning so that source code would become easier to maintain.

[2]
Using `var1' and `var2' as identifiers for named arguments is syntactically
correct, but unwise.

First, a single space in between either changes the meaning (in the function
body), or renders the source code syntactically incorrect (both in the
argument list and in the function body). `var' is a keyword after all.

Second, no meaning can be retrieved from those identifiers except of the
position in the argument list, without analyzing the entire source. As
it appears that `var1' should refer to an object, it deserved at least an
indication of that, like oVar1. Even better would have been an indication
in the identifier as to what kind of object is expected (oCollection). As
for `var2', it does not seem to be used here, so as well may be omitted.
var mytable = document.getElementById("myTBL");
See <URL:http://pointedears.de/scripts/test/whatami#inference>.

And use spaces to indent code, not tabs. Especially not when posting.
for (var i=myTBL.childNodes.length-1; i>0; i--){
for (var i = myTBL.childNodes.length; i--;)
{
myTBL.removeChild(myTBL.childNodes[i]);
}
What kind of ill-conceived block indentation is this?
for(var i=0; i<var1.length; i++){ ^^^^[2] var tr = document.createElement('TR');
var td1 = document.createElement('TD');
var a1 = document.createElement('A');
a1.appendChild(document.createTextNode('This is the link'));
a1.setAttribute=("HREF","#"); ^^^^^^^^^^^^^^
You want to make this a call instead of an assignment at first, and then
use the recommended shortcut property instead.

a1.href = "#";

But there is no point to this a[href] element anyway (you do not want a
hyperlink), so you better create an input[type="button"] element instead,
and format it with CSS if necessary.
a1.onclick=function(){alert(var1[i].value)};
What's the point?
td1.appendChild(a1);
tr.appendChild(td1);
}
}

Error: var1[...] is null or not an object
Probably that is the truth. Since you do not show the call, it is
impossible to say.[2]

However, one can say that this code is obviously written by someone who does
not know what they do:
And if put this :
a1.setAttribute=("onclick","var1[i].value");
this show me the result but on load and not onclick !!!


ISTM you have a lack of basic knowledge about the language to say the least.

`setAttribute' is a method, a function-property. It needs to be _called_.
The Call Operator (or the argument list) is delimited by `(' and `)', not
`=(' and `)':

a1.setAttribute("onclick","var1[i].value");

`=', on the other hand, is an assignment operator:

// Just a syntax example, nothing that would work in the real world!
a1.onclick = 1;

You cannot assign to a CallExpression because that is only allowed
right-hand side:

// syntax error
a1.setAttribute("onclick","var1[i].value") = "foo";

However, you can assign to an identifier of a method:

// see above, it is only pretty-printed
a1.setAttribute = ("onclick", "var1[i].value");

The meaning is entirely different, though. If the object would allow for
[[Put]] access, you would _replace_ the reference, thereby rendering the
method no longer working. Now, what you assigned is an expression, equal
to

a1.setAttribute = "onclick", "var1[i].value";

Iff replacing the reference was successful, `a1.setAttribute' would have the
value of the Comma Expression (the comma is an operator), which is the last
value of the comma-separated list; that is "var1[i].value". Of course that
string value cannot be called.

If replacing was unsuccessful, because the object referred to with `a1' is a
host object, as per specification all bets are off. Nothing may happen, or
you may receive an error message (probably you did but did not notice it),
or your computer may explode ;-)

As for the "to work" part of your source code, you can assign a string value
(or any other value that is not a reference to a callable object) to an
intrinsic event handler value, but it does not have an effect:

// is not supposed to have an effect
a1.onclick = "var1[i].value";

A Function object reference (event listener) is expected to be assigned to
the proprietary event handler property (see your first attempt above), or
added to the list of event listeners for an object:

a1.addEventListener('click', function() { ... }, false);

The latter is the standards compliant approach, as defined in the W3C DOM
Level 2 Events Specification. In the IE DOM, there is another proprietary
variant possible:

a1.attachEvent('onclick', function() { ... });

However, the latter's meaning is slightly different from the former's.

Please read the FAQ and search the archives before you post:

<URL:http://jibbering.com/faq/>
PointedEars
--
This above all: To thine own self be true.
-- William Shakespeare (1564-1616)
May 29 '06 #2

P: n/a
Thomas 'PointedEars' Lahn wrote:
Fr*********@gmail.com wrote: <snip> What kind of ill-conceived block indentation is this?
for(var i=0; i<var1.length; i++){

^^^^[2]
var tr = document.createElement('TR');
var td1 = document.createElement('TD');
var a1 = document.createElement('A');
a1.appendChild(document.createTextNode(
'This is the link'));
a1.setAttribute=("HREF","#"); <snip> a1.onclick=function(){alert(var1[i].value)}; <snip> td1.appendChild(a1);
tr.appendChild(td1);
}
}

Error: var1[...] is null or not an object


Probably that is the truth. Since you do not show the
call, it is impossible to say.[2]

<snip>

I assumed that the error happened when the link was clicked, and that
would certainly produce that error, which is true in that context. The
loop counter - i - having incremented beyond whatever content - var1 -
may have had before any event handler could be called.

Richard.
May 29 '06 #3

P: n/a
Thomas, thanks for your reply.

[1] [2] -> I just "rewrite" my function to make it more "general",
sorry for your comments about var and space and ....
About identation, I simply forgot to review my post before sending.
Thanks to you, my problem was because of `=` in my setAttribute
method.

But please, don't be so prideful, it's really ridiculous...

May 29 '06 #4

P: n/a
Fr*********@gmail.com wrote:
Thomas, thanks for your reply.
I would have said "you are welcome" if there had not been more.
[1] [2] ->
Are you referring to something?
I just "rewrite" my function to make it more "general",
Pardon?
sorry for your comments about var and space and ....
*You* are sorry for *my* comments?
About identation, I simply forgot to review my post before sending.
A good idea is not to use tabs in the first place. There are a number of
decent editors out there that can handle indentation with spaces well. But
maybe that's just me.
Thanks to you, my problem was because of `=` in my setAttribute
method.
You don't say!
But please, don't be so prideful, it's really ridiculous...


Ridiculous are people like you who are being told what is expected of
them here, then don't care, and complain although they got good advice
for free anyway. People who think of this Usenet discussion group as
a cheap help desk (on the Web).
Score adjusted

PointedEars
--
#define QUESTION ((bb) || !(bb))
// William Shakespeare (if he would have been a hacker ;-))
May 29 '06 #5

P: n/a
No problem, big boss... but you go wrong on topic here, nothing
political, don't be embittered.
You don't know me, and make philosophy on how people are after 3 lines
in 2 posts.
Really funny

Score adjusted too

Fred

May 29 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.