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

166
Visualizações
document.querySelector not selecting class

I can't seem to get this to work

app.js:

    const api = 'xxxxxxxx';

    function ySearch(e) {
    const url = 'https://www.googleapis.com/youtube/v3/search/? 
    part=snippet&key='+api+'&q=test&maxResults=20';

    document.querySelector('.output').textContent = url;
 
    }

html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <input type="text">
        <button>Search</button>
        <div class="output"></div>
        <script src="app.js"></script>
     </body>
</html>

Expected: When I reload the html, I get to see the url value. But the output class div is not being populated for some reason. What could be going wrong?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You aren't calling the function:

const api = 'xxxxxxxx';

function ySearch(e) {
  const url = 'https://www.googleapis.com/youtube/v3/search/?part=snippet&key=' + api + '&q=test&maxResults=20';

  document.querySelector('.output').textContent = url;

}

const button = document.querySelector('button');
button.addEventListener('click', ySearch)
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>

<body>

  <input type="text">
  <button>Search</button>
  <div class="output"></div>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to call ySearch for it to run, so you need an event listener on the button, to handle the click:

function ySearch(e) {
    const url = 'https://...';
    document.querySelector('.output').textContent = url;
}

// add event listener to button 
document.getElementById('searchButton').addEventListener('click', ySearch)
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script src="app.js" async defer></script>
</head>
<body>
  <input type="text">
  <button id="searchButton" >Search</button>
  <div class="output"></div>
</body>
</html>

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