1

I have the following code part written by my friend using jQuery in a project and I am trying to convert this to pure javascript so I can remove the jQuery file. I specially do not understand the .error() part. I have a very limited knowledge with Javascript and any help would be greatly appreciated.

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)
}
2
  • what is imageCell ? Commented May 10, 2020 at 1:23
  • It is a small app that was written to ping some websites using javascript. Similar to the concept of this. jsfiddle.net/GSSCD/203 Commented May 10, 2020 at 1:27

2 Answers 2

2

You can turn your jQuery into pure JS like below.

var a = document.getElementById("#myImage");
a.onerror = function(e){
  //display error
}; 
a.setAttribute("src", n)
Sign up to request clarification or add additional context in comments.

Comments

1
var a = $("#myImage");
    a.error(e), a.attr("src", n)

The .error(e) is the function that should handle the error. e is passed into the tripDestination function.

Again with a.attr("src", n ) , it's assigning what's in n to the src attribute of the class "myImage"

I'm assuming there's a img tag <img class="myImage"> .

The javascript version of changing the src attribute is:

document.getElementById("myImage").src = "hackanm.gif";

you can do this to your <img> tag : <img id="myImage">

Regarding error handling for this:

var imgele = document.getElementById("myImage");

imgele.onerror = function(e){
  // console.log()
}; 

imgele.src = 'newimage.jpg';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.