TypeError After Upgrading jQuery

Viewed 122

I just upgrade one of my project to the latest jQuery version and it stopped working. I was using jQuery v1.12.4 and it was working fine. This issue came after switching to 3.5.1.

On the console log, it says:

TypeError: a.error is not a function

And my affected code part:

var imageCell = $("#imageCell");

function tripDestination(t, e) {
    var n = Math.floor(0x10000000000000000 * Math.random()).toString(36);
    n = t + "my?x=" + n, imageCell.empty(), imageCell.html("<img id='myImage' style='display: none'>");
    var a = $("#myImage");
    a.error(e), a.attr("src", n)
}

Any help would be greatly appreciated.

3 Answers

As the Jquery documentation says,This API has been removed in jQuery 3.0; please use .on( "error", handler ) instead of .error( handler ) and .trigger( "error" ) instead of .error().

The error() has no more remained a built-in function for 3.0 Versions

Change your code to

var imageCell = $("#imageCell");

function tripDestination(t, e) {
    var n = Math.floor(0x10000000000000000 * Math.random()).toString(36);
    n = t + "my?x=" + n, imageCell.empty(), imageCell.html("<img id='myImage' style='display: none'>");
    var a = $("#myImage");
    a.on('error',function(e){...}), a.attr("src", n)
}

As mentioned here api.jquery.com/error, error() is deprecated and should be replaced with .trigger("error"):

Note: This API has been removed in jQuery 3.0; please use .on( "error", handler ) instead of .error( handler ) and .trigger( "error" ) instead of .error().

Hi the error funcion was removed in the jquery v 3.0, please test the following code:

$("#myImage" ).on("error",function() {
    alert("Handler for .error() called." );
}).attr( "src", "missing.png" );

Jquery Documentation

Related