0

so i am playing around with firefox addons. but this isnt important for this case cause its just simple dom javascript. so basically i want to give every image on an website an eventlistener that listens for clicks and if it gets one it should return the src of the clicked image. when i run my current code i'll get "undefined" as an result. I also already tried passing "images[i]" to the clickImg function at the listener but then i get all src values from all images on the current page, but that not what i want. I only want to get the src of the clicked image.

thanks for looking into this :)

const initSelMode = (request, sender, sendResponse) => {
    var images = document.querySelectorAll("img")
    for(var i = 0; i < images.length; i++){
        images[i].dataset.clickp = i
        images[i].addEventListener("click", clickImg)
  }
}

const clickImg = () => {
    var body = document.querySelector("body")
    var obj = document.createElement("p")
    obj.innerText = this.src
    body.appendChild(obj)
}

browser.runtime.onMessage.addListener(initSelMode)
CruZer0
  • 47
  • 5

0 Answers0