Ramzi Khahil
Ramzi Khahil

Reputation: 5052

Delegate events from an object to another

I want to delegate an event from one element to another. I have this simple test code, and I get an exception:

Error: UNSPECIFIED_EVENT_TYPE_ERR: DOM Events Exception 0 ` at line 12 (the dispatch)

I have read this, but the commented line before it, makes no difference. How can I make this work?

Code:

<!DOCTYPE html>
<html>
  <head>
    <script src="jquery.js"></script>
    <script>
    function init(){}

    function delegator(e){
        alert('the first');
        var other = document.getElementById('myCanvas2');
        //e.trigger = other;
        other.dispatchEvent(e);
    }

    $("#myCanvas").live('dblclick', delegator);
    $("#myCanvas2").live('dblclick', function(){alert('the second');});
    </script>
  </head>
  <body>
    <canvas id="myCanvas" style="width:1200px; height:600px; border: solid black 1px;"></canvas>
    <canvas id="myCanvas2" style="width:1200px; height:600px; border: solid black 1px;"></canvas>
  </body>
</html>

Upvotes: 3

Views: 173

Answers (1)

Naftali
Naftali

Reputation: 146300

  1. .live was deprecated a while ago.

  2. Since you are using jQuery use .trigger(...)

    function delegator(e){
        alert('the first');
        $('#myCanvas2').trigger(e);
    }
    
    $("#myCanvas").on('dblclick', delegator);
    $("#myCanvas2").on('dblclick', function(){alert('the second');});
    

    Demo: http://jsfiddle.net/maniator/zbJBd/

Upvotes: 4

Related Questions