Onclick and DOM problem

F

Fred.Grieco

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);
}
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.value)};
td1.appendChild(a1);
tr.appendChild(td1);
}
}

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

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

Help!
Thanks

Fred
 
T

Thomas 'PointedEars' Lahn

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);
}


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.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.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.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.value") = "foo";

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

// see above, it is only pretty-printed
a1.setAttribute = ("onclick", "var1.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.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.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.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
 
R

Richard Cornford

Thomas said:
(e-mail address removed) wrote:
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","#");
a1.onclick=function(){alert(var1.value)};

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

Fred.Grieco

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

Thomas 'PointedEars' Lahn

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
 
S

Sega

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
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top