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

  • SEARCH
  • Home
  • 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 9065931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:37:36+00:00 2026-06-16T16:37:36+00:00

I am making an web app in which I am creating rows dynamically and

  • 0

I am making an web app in which I am creating rows dynamically and adding click listener to each dynamically added row. But I could not figure out how to get the rows index and to pass some variable to the listener.

for(var i=0; i<len; i++)  {      
    var TABLE = document.getElementById('tableId');
    var BODY = TABLE.getElementsByTagName('tbody')[0];

    //making a row
    var TR = document.createElement('tr');
    TR.className = "section group";

    //creating first column
    var TD = document.createElement('td');
    TD.className = "col span_1_of_3";

    //adding click listener
    url_array[count] = result.details[count].video_url;
    TR.addEventListener('click', function(e) {
        console.log(count,e);  //     ---------> 1 
    });

    count++;
}

In line indicated ---> 1, I have a global variable count initialized to zero and its value is last row index.

  • 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. Editorial Team
    Editorial Team
    2026-06-16T16:37:37+00:00Added an answer on June 16, 2026 at 4:37 pm

    This is a scoping issue. When the click handler fires, then count is set to the last one. Try wraping your .addEventListener method with an anonymous function:

    (function(count){
        TR.addEventListener('click', function(e) {
            console.log(count,e);  //     ---------> 1 
        });
    })(count);
    

    This will work, because functions create seperate scope unlike for loop.

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

Sidebar

Related Questions

I am practicing making a web app which tries to read the user's twitter
I'm making a Django web-app which allows a user to build up a set
I am making a simple web-app which requires login for the admin page. I
I'm making an android app which has to frequently call some .net web services
I am making an iPhone app which has its database on the web server.
I'm making a small web app in which a user enters a server URL
I am using rails to build a web app which provides each business with
I'm making a web app which display a HTML table. The app is written
Im making a windows phone app which uses a WebBrowser. Im using this web
I am making a web app which needs to store a user's email and

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.