Search code examples
jqueryprototype

Prototype .observe says attachEvent is not a function


Can somebody tells me, why do I get attachEvent is not a function error with this - jsFiddle?

var j = jQuery.noConflict();

function myHandler() {
  alert('triggered');
}
Event.observe(j('#xxx'), 'change', myHandler);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prototype/1.7.3/prototype.min.js"></script>

<input type="text" id="xxx" value="123" />


Solution

  • Event.observe() expects DOM element or id of the element so either pass id i.e. xxx directly or use .get(index)/[index] get the reference to underlying DOM element at index and pass it to method.

    var j = jQuery.noConflict();
    
    function myHandler() {
      console.log('triggered');
    }
    Event.observe('xxx', 'change', myHandler);
    //Event.observe(j('#xxx').get(0), 'change', myHandler);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/prototype/1.7.3/prototype.min.js"></script>
    
    <input type="text" id="xxx" value="123" />