# Using functions in addEventListener - scope issues

**URL:** <https://forum.kirupa.com/t/using-functions-in-addeventlistener-scope-issues/236099>\
**Category:** web dev\
**Created:** [August 17, 2007, 3:41am UTC](https://forum.kirupa.com/t/using-functions-in-addeventlistener-scope-issues/236099 "2007-08-17T03:41:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![boondocksaint2k](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/boondocksaint2k/32/3453_2.png) [@boondocksaint2k](https://forum.kirupa.com/u/boondocksaint2k)\
**Post date:** [August 17, 2007, 3:41am UTC](https://forum.kirupa.com/t/using-functions-in-addeventlistener-scope-issues/236099/1 "2007-08-17T03:41:49Z")

</div>

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

[FONT=Courier New] function albums() {[/FONT][INDENT][FONT=Courier New] …  
xrequest.onreadystatechange = function() {[/FONT][INDENT][FONT=Courier New] if ( xrequest.readyState == 4 ) {[/FONT][INDENT][FONT=Courier New] if ( xrequest.status == 200 ) {[/FONT][INDENT][FONT=Courier New] …  
for (var i = 0; i \< xrows.length; i++) {[/FONT][INDENT][FONT=Courier New] var album = document.createElement(‘img’);  
album.src = …  
album.alt = xrows\*.getAttribute(‘album\_name’);  
album.id = xrows\*.getAttribute(‘album\_id’);  
document.getElementById(‘albums’).appendChild(album);  
album.addEventListener(‘click’, function() { album(this.id); }, false);  
[/FONT] [/INDENT][FONT=Courier New]}  
[/FONT] [/INDENT][FONT=Courier New]}  
[/FONT] [/INDENT][FONT=Courier New]}  
[/FONT] [/INDENT][FONT=Courier New]}  
[/FONT] [/INDENT][FONT=Courier New]}  
function album(id) { alert(id); }[/FONT]

It doesn’t work. And rightfully so, of course. My question is, how do I access the album function?

Any help would be greatly appreciated; thank you for your time.
