Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: $(...).addEventListener is not a function

Tags:

javascript

setTimeout(function() {
        $.ajax({
        url : "handlers/H_AnnotationHandler.php",
        data : "case_id=<?=$case_id?>&plink=<?=$plink?>&mode=get",
        type : "post",
        dataType : "json",
        success : function (response) {
            if (!response.error) {
                annotation_length = response.annots.length;
                for (var i = 0; i < response.annots.length; i++) {
                    var elt  = document.createElement("div");
                    elt.id = "runtime-overlay" + i;
                    elt.className = "highlight";
                    viewer.addOverlay({
                        element: elt,
                        location : viewer.viewport.imageToViewportRectangle(parseInt(response.annots[i].rect_x), parseInt(response.annots[i].rect_y), parseInt(response.annots[i].rect_w), parseInt(response.annots[i].rect_h))
                    });
                    $("#runtime-overlay"+i).attr("onclick", "$.clickOverlay('"+i+"')");
                }
            }               
        }
    });
    }, 3000);

    $.clickOverlay = function(whichOverlay) {
            var flag = 0;
            $("#runtime-overlay"+whichOverlay).addEventListener("mousedown", function(){
                flag = 0;
            }, false);
            $("#runtime-overlay"+whichOverlay).addEventListener("mousemove", function(){
                flag = 1;
            }, false);
            $("#runtime-overlay"+whichOverlay).addEventListener("mouseup", function(){
                if(flag === 0){
                    console.log("click");
                }
                else if(flag === 1){
                    console.log("drag");
                }
            }, false);
    }

Why i get an type error for addeventlistener ? Can you help me, i try to understand click or drag. so i added that functions in my click event : How to distinguish mouse "click" and "drag"

ERROR : Uncaught TypeError: $(...).addEventListener is not a function

like image 508
Levent Tulun Avatar asked Jan 13 '16 13:01

Levent Tulun


People also ask

Why is addEventListener not a function?

The "addEventListener is not a function" error occurs for multiple reasons: calling the method on an object that is not a valid DOM element. placing the JS script tag above the code that declares the DOM elements. misspelling the addEventListener method (it's case sensitive). Here is an example of how the error occurs.

Why is element getelementsbytagname() throwing an error?

This throwing an error saying that it is not a function. Where is my mistake? The function Element.getElementsByTagName () returns a HTMLCollection of elements, you need to loop over it to bind the events.

Is it possible to add an event listener to a button?

You have a variable named button (singular), but you’re adding event listener to buttons (plural). Yes, but I tried with my variable button (singular). It still doesn’t work. Then it’s probably button 's id in HTML.


1 Answers

As a comment pointed out, You are trying to use the "vanilla" javascript way to add an eventlistener on a jQuery object.

$.clickOverlay = function(whichOverlay) {
        var flag = 0;
        $("#runtime-overlay"+whichOverlay).addEventListener("mousedown", function(){
            flag = 0;
        }, false);
        $("#runtime-overlay"+whichOverlay).addEventListener("mousemove", function(){
            flag = 1;
        }, false);
        $("#runtime-overlay"+whichOverlay).addEventListener("mouseup", function(){
            if(flag === 0){
                console.log("click");
            }
            else if(flag === 1){
                console.log("drag");
            }
        }, false);
}

instead try:

$.clickOverlay = function(whichOverlay) {
        var flag = 0;
        $("#runtime-overlay"+whichOverlay).on("mousedown", function(){
            flag = 0;
        });
        $("#runtime-overlay"+whichOverlay).on("mousemove", function(){
            flag = 1;
        });
        $("#runtime-overlay"+whichOverlay).on("mouseup", function(){
            if(flag === 0){
                console.log("click");
            }
            else if(flag === 1){
                console.log("drag");
            }
        });
}
like image 85
Zesar Avatar answered Oct 12 '22 23:10

Zesar