Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 88349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:32:46+00:00 2026-05-10T22:32:46+00:00

I need to pick up list items from an list, and then perform operations

  • 0

I need to pick up list items from an list, and then perform operations like adding event handlers on them. I can think of two ways of doing this.

HTML:

    <ul id='list'>        <li id='listItem-0'> first item </li>        <li id='listItem-1'> second item </li>         <li id='listItem-2'> third item </li>      </ul> 
  1. Using the IDs-

    for(i=0;i<3;i++) {    document.getElementById('listItem-'+i).addEventListener('click',foo,false); } 
  2. Using childNodes property-

    for(i=0;i<3;i++) {   document.getElementById('list').childNodes[i]      .addEventListener('click',foo,false); } 

The reason i’m using the first approach is that in the function foo, if I want the index at which the item is located in the list, i can do it by splitting the id –

    function foo()     {       tempArr = this.id.split('-');             index = tempArr[tempArr.length-1]; // the last element in the array     } 

I can’t think of a way of doing it by using the second method, that is, without using the id naming scheme.

The questions:

  1. How do I get the index using the second method or any better method
  2. Are there some very bad affects of following the first method ?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:32:46+00:00Added an answer on May 10, 2026 at 10:32 pm

    You can avoid adding event handlers to each list item by adding a single event handler to the containing element (the unordered list) and leveraging the concept of event bubbling. In this single event handler, you can use properties of the event object to determine what was clicked.

    It appears that you are wanting to map data in an array to list items. While parsing out an array index out of the list item id would work, another approach would be to store a ‘key’ value on the list item as an expando, and use javascript object properties to do a lookup for your data.

    Partial Example:

    <li key='myKey'>  //oData is a object (eg. var oData = {};) that has been populated with properties //whose value is the desired data (eg. oData['myKey'] = 123;)  alert(oData[event.srcElement.key]); // alerts 123 

    As far as bad effects of the first technique you showed, one bad effect is that with many list items you end up with many event handlers being defined, which at some point would have an impact on performance.

    Also note that you may be unintentionally creating a global var in your loops by omitting the var keyword for ‘i’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 274k
  • Answers 274k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the java.net classes. Below is a simple example using… May 13, 2026 at 2:25 pm
  • Editorial Team
    Editorial Team added an answer You can certainly write an ActiveX plugin for IE in… May 13, 2026 at 2:25 pm
  • Editorial Team
    Editorial Team added an answer It really depends on how you are accessing the database,… May 13, 2026 at 2:25 pm

Related Questions

I am trying to build an Infopath form which populates fields with data back
Does anyone have any good starting points for me when looking at making web
I have a custom content type in SharePoint which has an approval workflow associated
Having read up on quite a few articles on Artificial Life (A subject I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.