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

265
Visualizações
How do I use Javascript to return the text of the paragraph clicked as a usable variable?

I'm new to Javascript and self taught, my english is a little rough too, please bare with me. I've searched all over and tried everything I can.

I'm trying to make a table of content where clicking it brings up a form modal.. I want that when the form opens the innerHTML of the button clicked becomes a variable on it's own. It will be submitted alongside other form inputs.. I just want to extract the paragraph text and use it as a variable that can be recall or something like that. I can't seem to get the topicHeading variable out of the for loop, I get undefined.

var topicHeading;
var topicButtons = document.querySelectorAll('p[class^=topicName]');

for (var i = 0; i < topicButtons.length; i++) {
  topicButtons[i].addEventListener('click', function() {
    var topicHeading = this.innerHTML;
    return topicHeading;
  });
};

console.log(topicHeading);
<div class="wallet-buttons" id="walletsList">

  <div id="Topic1" class="button-section" onclick="opendetails()">
    <i class="active-icon"></i>
    <div class="btn-name">
      <p class="topicName">Topic 1</p>
    </div>
    <div class="btn-img">
      <img src="img/topicimg.svg" alt="Topic Image">
    </div>
  </div>

  <div id="Topic2" class="button-section" onclick="opendetails()">
    <i class="active-icon"></i>
    <div class="btn-name">
      <p class="topicName">Topic 2</p>
    </div>
    <div class="btn-img">
      <img src="img/topicimg.svg" alt="Topic Image">
    </div>
  </div>

  <div id="Topic3" class="button-section" onclick="opendetails()">
    <i class="active-icon"></i>
    <div class="btn-name">
      <p class="topicName">Topic 3</p>
    </div>
    <div class="btn-img">
      <img src="img/topicimg.svg" alt="Topic Image">
    </div>
  </div>

  <div id="Topic4" class="button-section" onclick="opendetails()">
    <i class="active-icon"></i>
    <div class="btn-name">
      <p class="topicName">Topic 4</p>
    </div>
    <div class="btn-img">
      <img src="img/topicimg.svg" alt="Topic Image">
    </div>
  </div>

</div>

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

0

Get rid of var in the function so you assign the global variable instead of a local variable.

To view the variable, show it from a function that runs after the user has clicked on a topic. You can use another button for that.

var topicHeading;
var topicButtons = document.querySelectorAll('p.topicName');

for (var i = 0; i < topicButtons.length; i++) {
  topicButtons[i].addEventListener('click', function() {
    topicHeading = this.innerHTML;
  });
};

document.getElementById("show").addEventListener("click", function() {
  console.log(topicHeading);
})

function opendetails() {
}
<div class="wallet-buttons" id="walletsList">

  <div id="Topic1" class="button-section" onclick="opendetails()">
    <i class="active-icon"></i>
    <div class="btn-name">
      <p class="topicName">Topic 1</p>
    </div>
    <div class="btn-img">
      <img src="img/topicimg.svg" alt="Topic Image">
    </div>
  </div>

  <div id="Topic2" class="button-section" onclick="opendetails()">
    <i class="active-icon"></i>
    <div class="btn-name">
      <p class="topicName">Topic 2</p>
    </div>
    <div class="btn-img">
      <img src="img/topicimg.svg" alt="Topic Image">
    </div>
  </div>

  <div id="Topic3" class="button-section" onclick="opendetails()">
    <i class="active-icon"></i>
    <div class="btn-name">
      <p class="topicName">Topic 3</p>
    </div>
    <div class="btn-img">
      <img src="img/topicimg.svg" alt="Topic Image">
    </div>
  </div>

  <div id="Topic4" class="button-section" onclick="opendetails()">
    <i class="active-icon"></i>
    <div class="btn-name">
      <p class="topicName">Topic 4</p>
    </div>
    <div class="btn-img">
      <img src="img/topicimg.svg" alt="Topic Image">
    </div>
  </div>

</div>
<p>
<button id="show">Show topic</button>
</p>

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