I am using Javascript and am creating my function within a closure, the problem I’m having is getting the id of the button the user has pressed.
The following code works when not in the closure, but when I place it inside it doesn’t seem to work.
Please help, thanks in advance for any help.
code to get id…
var id = event.target.id;
within closure…
var closure = (function(){
var id;
return{
getId: function(){
id = event.target.id;
}
};
}());
You’re merely assigning
idto some value, butidis not accessible due to the closure.You should instead
returnthe value so that you can get the result and actually use it:You also don’t need
var id;since there is no reason for a local variable – you seem to just want to get the current ID. Theidvariable is nowhere accessible and you’re also not accessing it inside the closure, so it doesn’t serve any purpose here.