Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!