Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return value from addEventListener

I use Javascript to catch the x and y position for when user clicks a link.

I can make it work, but I want it to return the two values to function init() when it is called.

How can I do it?

<script type="text/javascript">

  document.addEventListener("DOMContentLoaded", init, false);

  function init()
  {
    var canvas = document.getElementById("canvas");
    canvas.addEventListener("mousedown", getPosition, false);
    
    // how can I get the return values here?

  }

  function getPosition(event)
  {
    var x = new Number();
    var y = new Number();
    var canvas = document.getElementById("canvas");

    if (event.x != undefined && event.y != undefined)
    {
      x = event.x;
      y = event.y;
    }
    else
    {
      x = event.clientX + document.body.scrollLeft +
          document.documentElement.scrollLeft;
      y = event.clientY + document.body.scrollTop +
          document.documentElement.scrollTop;
    }

    x -= canvas.offsetLeft;
    y -= canvas.offsetTop;

    alert("x: " + x + "  y: " + y); // here can print the correct position
    
    // if I add the two values here, and return them. How can I receive the values in funciton init()
    // var clickPosition={"x":x, "y":y};
    // return clickPosition;
  }

</script>
like image 263
jdleung Avatar asked Nov 03 '15 14:11

jdleung


People also ask

Does addEventListener return anything?

The addEventListener() method (when applied to the document object of the JavaScript DOM) attaches an event handler to the document. It has the following syntax with the following parameters. It does not return any value. Some earlier versions of some major browsers do not support this method.

Is addEventListener a callback function?

addEventListener('click', () => { console. log('element clicked'); }); The code queries the DOM, grabs a specific element and adds a click event listener to it using addEventListener . addEventListener accepts the event type, a listener callback function and an options or useCapture parameter.

Is Onclick the same as addEventListener?

addEventListener can add multiple events to a particular element. onclick can add only a single event to an element. It is basically a property, so gets overwritten.

What is addEventListener () in JavaScript?

The JavaScript addEventListener() method allows you to set up functions to be called when a specified event happens, such as when a user clicks a button. This tutorial shows you how you can implement addEventListener() in your code.

What is the value of currenttarget in addEventListener?

If attaching a handler function to an element using addEventListener () , the value of this inside the handler is a reference to the element. It is the same as the value of the currentTarget property of the event argument that is passed to the handler.

Why doesn't addeventhandler work after the event happens?

The event handler function won't run until the event happens. By that time, the function that called addEventHandler will have finished running and returned. The event handler function needs to either deal with the data itself, or call other functions to do it.

What happens when an eventlistener is added to an eventtarget?

If an EventListener is added to an EventTarget while it is processing an event, that event does not trigger the listener. However, that same listener may be triggered during a later stage of event flow, such as the bubbling phase.


2 Answers

Where you have the comment, you will never be able to access the variables, the event has not occurred yet.

Instead, what you can do is pass an anonymous function to the event handler, call your method which returns a value and use it as appropriate

function init()
{
    var canvas = document.getElementById("canvas");
    canvas.addEventListener("mousedown", function(event){
        var result = getPosition(event);

        // result is your return value
    }, false);

}
like image 143
Jamiec Avatar answered Sep 21 '22 11:09

Jamiec


You can't.

JavaScript isn't capable of time travel.

The event handler function won't run until the event happens. By that time, the function that called addEventHandler will have finished running and returned.

The event handler function needs to either deal with the data itself, or call other functions to do it. The data has to travel forwards, it can't go back.

like image 26
Quentin Avatar answered Sep 18 '22 11:09

Quentin