473,325 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Using functions in addEventListener - scope issues

I'm very new to JavaScript and I have a problem with the following:

Expand|Select|Wrap|Line Numbers
  1. function albums() {
  2.     ...
  3.     xrequest.onreadystatechange = function() {
  4.         if ( xrequest.readyState == 4 ) {
  5.             if ( xrequest.status == 200 ) {
  6.                 ...
  7.                 for (var i = 0; i < xrows.length; i++) {
  8.                     var album = document.createElement('img');
  9.                     album.src = ...
  10.                     album.alt = xrows[i].getAttribute('album_name');
  11.                     album.id = xrows[i].getAttribute('album_id');
  12.                     document.getElementById('albums').appendChild(album);
  13.                     album.addEventListener('click', function() { album(this.id); }, false);
  14.                 }
  15.             }
  16.         }
  17.     }
  18. }
  19. function album(id) { alert(id); }
  20.  
It doesn't work. And rightfully so, of course. I get the following message: "album is not a function." My question is, how do I access the album function?

Any help would be greatly appreciated; thank you for your time.
Aug 17 '07 #1
3 1681
acoder
16,027 Expert Mod 8TB
Please use CODE tags for posting code. Thanks.

Don't use the same name for the variable and the function.
Aug 17 '07 #2
Please use CODE tags for posting code. Thanks.

Don't use the same name for the variable and the function.
Sorry about that.

And thank you very much.
Aug 17 '07 #3
acoder
16,027 Expert Mod 8TB
Sorry about that.

And thank you very much.
So is that the problem solved?
Aug 18 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: NotGiven | last post by:
Below is a code snippet I am having a hard time with. I rerquire code1.php and code 2.php. code1.php contains some variables the code needs. code2.php contains some functions I call from the...
5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
1
by: anton muhin | last post by:
In addition to s hack, I'd like to suggest the following code. Any comments are highly appreciated: class Scope(object): pass def accumulator(): scope = Scope() scope.n = 0 def f(n):
9
by: Neelesh | last post by:
I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends") where it is mentioned that What I observe is a bit different - it is fine that there is an error for Xform, but the reason...
1
by: G Fernandes | last post by:
Scope is a property of identifiers and defines where they are visible in a source file. Why then do most writings on C also use the word "scope" to refer to a property of different points of a...
3
by: Chris | last post by:
Hi, I am learning object oriented programming. I am always running into scope issues in my C# ASP.NET applications. example: a public void methods: My intellisense does not does up for...
7
by: nephish | last post by:
lo there all, i have an app that runs three classes as threads in the same program. some of them need to be able to share some functions. Can i set a function before i define a class and have...
3
by: bxstylez | last post by:
rewrite this program (sum,average) using functions, possibly arrays? #include<stdlib.h> #include<stdio.h> main() { FILE *finpt, *foutpt;
3
by: TamaThps | last post by:
I have to write a program that lets the user play the game of Craps. As of right now it is incomplete and I have run into some errors. It's my first time using functions instead of the program all...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.