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