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

208
Visualizações
Get nested button id in header

I'm trying to reference my button (which is nested inside my header/nav), to make an event onclick, but I can't seem to get it to work.

Tried nesting getElementByName with getElementById tried some querySelector and querySelectorAll

Tried:

var x = document.getElementsByClassName('hdr');
var y= x.querySelector('hdrBtns');
z = y.getElementById('singUp');

Tried:

const brt = document.querySelectorAll('.hdr .hdrBtns');
const dac = brt.getElementById('signUp');

Code Underneath:

 <body>
        <header class="hdr">
            <div class="hLogo">
                <h1>
                    Test
                </h1>               
            </div>

            <nav class="hdrBtns">
                <button id="singUp">Sign Up</button>
                <button id="singIN">Sign In</button> 
                
            </nav>
        </header>
     <script src="./script.js"></script>
    </body>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The NodeList returned by Element#querySelectorAll() or getElementsByClassName() does not have Methods other Elements have, like getElementById() or querySelector().

Assuming you only have one Element with your particular ID, like you should have, just selecting the element by this will work:

let btn = document.querySelector("#singUp")

btn.addEventListener("click", () => alert("test"))
<header class="hdr">
  <div class="hLogo">
    <h1>
      Test
    </h1>
  </div>

  <nav class="hdrBtns">
    <button id="singUp">Sign Up</button>
    <button id="singIN">Sign In</button>

  </nav>
</header>

about 4 years ago · Juan Pablo Isaza Relatório

0

A lot ways to do that. But If you looking for a element which already has an id then would be the best to use getElementByID() function. Otherwise you can use querySelector in many different ways.

const btns = document.querySelectorAll('.hdr nav button');
let id1 = btns[0].getAttribute('id');
let id2 = btns[1].getAttribute('id');

console.log('v1',id1, id2)

const b1 = document.querySelector('.hdr nav button:first-child');
const b2 = document.querySelector('.hdr nav button:last-child');
console.log('v2', b1.getAttribute('id'), b2.getAttribute('id'));


const b3 = document.getElementById('singUp');
const b4 = document.querySelector('#singIN'); 
console.log('v3', b3.getAttribute('id'), b4.getAttribute('id'));
<body>
        <header class="hdr">
            <div class="hLogo">
                <h1>
                    Test
                </h1>               
            </div>

            <nav class="hdrBtns">
                <button id="singUp">Sign Up</button>
                <button id="singIN">Sign In</button> 
                
            </nav>
        </header>
     <script src="./script.js"></script>
    </body>

about 4 years ago · Juan Pablo Isaza Relatório

0

You simply have to use this code:

var signUp = document.getElementById('signUp');

signUp.addEventListener('click', () => {
  // Do something...
});
about 4 years ago · Juan Pablo Isaza Relatório
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