/    Sign up×
Community /Pin to ProfileBookmark

Problem with setting onClick event handler dynamically

Hello,
I am trying to set an onClick event handler dynamically for a TD element in my document. Here is the code I am using, which is part of a function called “tabAction”:

[CODE]
var num = 0;
var myObjColl2 = getElementsByClassName(‘slidertabs’, ‘td’, cont);
for (var i = 0, j = myObjColl2.length; i < j; i++) {
num = Right(myObjColl2[i].id, 1);
myObjColl2[i].onclick = function() {tabAction(true, myObjColl2[i], ‘menu’ + num)};
[/CODE]

The getElementsByClassName function does just what the name implies, and returns an array of elements that have that className.

The TD element originally contains the following onClick event handler as:

[CODE]<td align=”center” valign=”middle” id=”slider1″ onclick=”tabAction(true, this, ‘menu1’);” class=”slidertabs”>[/CODE]

Everything seems to work OK except after the code runs. When I click on a TD element that has been updated, I get an error message:

[B]‘Undefined’ is null or not an object[/B]

I think I have narrowed the problem down to the [B]myObjColl2[i][/B] in the code:

[CODE]myObjColl2[i].onclick = function() {tabAction(true, myObjColl2[i], ‘menu’ + num)};[/CODE]

How can I refer to the element object from which the function is called if I am assigning it dynamically?

Thanks for any help!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Mr_JApr 27.2007 — Give this a try

[CODE]var num = 0
var myObjColl2 = getElementsByClassName('slidertabs', 'td', cont)

for (var i = 0, j = myObjColl2.length; i < j; i++){

myObjColl2[i].num = Right(myObjColl2[i].id, 1)

myObjColl2[i].onclick = function(){
tabAction(true, myObjColl2[i], 'menu' + this.num)
}

}
[/CODE]
Copy linkTweet thisAlerts:
@jimsteele01authorApr 27.2007 — Unfortunately that didn't help. The 3rd argument is passing fine ('menu' + num), the problem is in the 2nd argument (myObjColl2[i]).



Thanks though!
Copy linkTweet thisAlerts:
@Mr_JApr 28.2007 — At line

var myObjColl2 = getElementsByClassName('slidertabs', 'td', cont)


what is the value of myObjColl2 supposed to be
Copy linkTweet thisAlerts:
@FangApr 28.2007 — How can I refer to the element object from which the function is called if I am assigning it dynamically?[/QUOTE]
myObjColl2[i].onclick = function() {tabAction(true, this, 'menu' + num)};
×

Success!

Help @jimsteele01 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...