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

219
Visualizações
js to communicate with dom in wordpress

I am trying to listen to a click event of a link using javascript inside a wordpress project. But I could notice, NodeList is getting empty. Seem to be it's having an issue when communicating with the DOM. My attempt is as below. I created a folder called theme and inside the index.php there, below lines were added.

<?php get_header(); ?>
<a href="#" class="btn btn-next width-50 ml-auto">Next1</a>
<a href="#" class="btn btn-next width-50 ml-auto">Next2</a>

Then inside the functions.php file imported the external scripting file as below.

<?php
 function pet_data1(){
  wp_enqueue_script('pet_adoption_style1', 
  get_stylesheet_directory_uri().'/script.js', array(), false, false);
 }
 add_action('wp_enqueue_scripts', 'pet_data1');
?>

Next the header.php file takes as below.

<!DOCTYPE html>
<html lang="en">
<head>
  <?php wp_head();?>
</head>
</html>

Finally, script.js file which is not given the expected output is as in below.

const nextBtns = document.querySelectorAll(".btn-next");
console.log("nextBtns",nextBtns)

When I print the nodeList, length is zero. But there should be 2 nodes which contains the btn-next class. So I think there should be an issue with wordpress structure which I have followed. Since I am new to wordpress, I need your support to fix this out.

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

0

The issue is not with "Wordpress structure", but because you're loading the script in <head>, but attempting to access elements in the document body before waiting for the DOM to be ready.

<script> tags in the <head> will be downloaded and executed, before the browser parses the rest of the HTML found after <head> This causes issues with your selectors since when the script is executed, those elements that would have matched your selectors do not exist in the DOM tree at runtime:

Script loading

Source: https://www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html

There are two solutions:

  1. Ensure that script.js is loaded in the footer, which means the DOM has been parsed and is ready, or
  2. Wrap all DOM querying logic in script.js in a callback that is invoked upon the firing of DOMContentLoaded event:
window.addEventListener('DOMContentLoaded', () => {
  const nextBtns = document.querySelectorAll(".btn-next");
  console.log("nextBtns",nextBtns)
});
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