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

256
Visualizações
Getting the whole content of a clicked <li> element

This is my HTML line:

<li onclick="return open_create_event_screen();">10</li>

I want to get the content of this < li > element (which is '10') through the JavaScript function (open_create_event_screen()) that opens once the < li > element is clicked. Is it possible to do so?

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

0

Old way, where we put inline events into our HTML. The key is to pass this into the function, which is a reference to the element that was acted upon, you could also pass event, and determine the clicked element from the target property (two different approaches).

const open_create_event_screen = (obj) => {
  console.log("opening create event screen");
  console.log(obj.innerText);
}
<li onclick="open_create_event_screen(this);">10</li>

Here's a more modern approach, where our html is cleaner, and we do the work of assigning the event handlers in javascript.

const open_create_event_screen = (obj) => {
  console.log("opening create event screen");
  console.log(obj.innerText);
}

const lis = document.querySelectorAll(".eventThing>li");
lis.forEach(li => 
  li.addEventListener("click", () => open_create_event_screen(li))
);
<ul class="eventThing">
  <li>10</li>
  <li>20</li>
</ul>

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