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

263
Visualizações
How 'this' is different on DIV and Input

I consoled out the this keyword (pls refer the code) but for div the console out was different and for Input tag it's different. Wondering why?

(function(){
    document.addEventListener('DOMContentLoaded', function(){
    const complement = new Complement();
    complement.generateRandomNumber();
    complement.events();
});

class Complement{
    constructor(){
        this.firstInt = document.getElementById('firstint') //this is div
        this.secondInt = document.getElementById('secondint') //this is input
        this.sumInt = document.getElementById('sumint')
    }

    events = function(){
        this.secondInt.addEventListener('keyup', this.checkFields)


    }
}
Complement.prototype.generateRandomNumber = function(){
    **console.log(this) //this refers to the Class Complement. why?**
    let randonFirstInt = Math.floor(Math.random()*100)
    
    //assign random number to first integer
    this.firstInt.textContent = randonFirstInt;
}

Complement.prototype.checkFields = function() {
    **console.log(this) //this refers to the input tag. why?** 
    let secondIntInput = this.value
    console.log(secondIntInput)
}

})()

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