0

Below is a simplified version of a code, which I am trying to make work for some hours now.

My aim is to get the id of the element, which a user clicks on, and pass the id value to a global variable targetId.

Unfortunately, although I manage to pass the id to a variable targetId, I can only access it within returnId function. If I try to access targetId outside of the function - it returns no value.

Why is that?

Thanks in advance for help! :)

var targetId;

function returnId(e) {
    targetId = e.target.id; 
}

element.addEventListener('click', returnId, false);
displayname
  • 1,153
  • 1
  • 9
  • 21

1 Answers1

2

This is an asynchronous call. You have to implement it differently. This has been answered multiple times...

Duplicate of : How do I return the response from an asynchronous call?

Community
  • 1
  • 1
ben
  • 3,558
  • 1
  • 15
  • 28