473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Onclick and DOM problem

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
5 2077
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Shock | last post by:
Hello everyone, I am having a problem with the program below. I have isolated the problem to the onclick event that is located throughout arrQuestions. The onclick event refers to a function...
5
by: Mike | last post by:
In my previous post, I wrote: > ... > GOAL: (very simple) Provide a hyperlink which, when clicked, > calls a javascript function which opens a new URL. > ... > PROBLEM: The following code...
17
by: Mike Gratee | last post by:
Is it possible to use JavaScript to cause the browser to click a link on a page and have the browser act exactly like the user had clicked on the link directly? In other words, I need to...
8
by: KS | last post by:
Just to show some code to show the consept. <img id="date" onclick="javascript:show_calendar();" src="/PlexSysWeb/images/show-calendar.gif" width=20 height=18 border=0> What i want the...
5
by: moondaddy | last post by:
I have a <a> element in a datagrid which wraps some asp.net labels. this element also has an onclick event which does not fire in netscape 6 (and perhaps other browsers for all I know...). Below...
11
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. ...
7
by: abs | last post by:
Hi everyone. Please, check my test code here: http://skocz.pl/jstest . The trouble is that no matter which span element I click, it alerts '3' and I'm wondering why not '1' for the first span,...
3
by: Michael_R_Banks | last post by:
I'm trying to dynamically build a table that allows users to remove rows when they click a corresponding button. For some reason, whenever I add the button to the table, it never fires the onclick...
2
by: stevemtno | last post by:
I've got a problem with a web page I'm working on. I have 4 modules - one of them has 2 tabs, two of them have 4 tabs. When the user clicks on the tabs, the content below them changes. However, when...
2
by: DavidGeorge | last post by:
In an earlier thread I recounted a problem I was having, but it took a while to reduce the problem to it's basic components and the issue became somewhat confused in reaching that point. I hope you...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.