Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
Pass "this" for an element inside an array

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)
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda