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

184
Visualizações
How is <button> read in JavaScript without using querySelector or getElementById?

I have written a JavaScript file that contains has an onclick method attached to an element in the HTML, but I never selected it in the JavaScript.

window.onload = function() {
    createSketchpad(0);
};

function createSketchpad(size) {
    const sketchpad = document.querySelector('.sketchpad');
    sketchpad.style.gridTemplateColumns = `repeat(${size}, 1fr)`;
    sketchpad.style.gridTemplateRows = `repeat(${size}, 1fr)`;
    for(var i = 0; i < Math.pow(size,2); i ++) {
        var gridIndex = document.createElement('div');
        gridIndex.style.backgroundColor = 'blue';
        sketchpad.insertAdjacentElement('beforeend', gridIndex);
    }
};

submitSizeButton.onclick = function chooseSketchpadSize() {
    var submitSizeInput  = document.querySelector('.submitSizeInput').value;
    createSketchpad(submitSizeInput);
};

HTML body

    <body>
        <div class="sketchpad">   
        </div>
        <input class="submitSizeInput"type="text"> 
      <button id="submitSizeButton" name="clickme" type="submit"> </button>


    <script src="index.js"> </script>
      
</body>

As seen above, submitSizeButton was never defined in the JavaScript, but in my HTML I have a button with an ID set to submitSizeButton. My question is, how come I do not have to use document.querySelector or document.getElementById to bring it to the DOM?

However, if this does not work with other elements such as the input class. How come that does not become a global attribute as well the same as the id does for the button?

Here is a full CodePen: https://codepen.io/fwan0/pen/GRymErp

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