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 119583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:40:34+00:00 2026-05-11T03:40:34+00:00

I am having problems with my JScript code. I am trying to loop through

  • 0

I am having problems with my JScript code. I am trying to loop through all of the rows in a table and add an onclick event. I can get the onclick event to add but have a couple of problems.

The first problem is that all rows end up getting set up with the wrong parameter for the onclick event.

The second problem is that it only works in IE.

Here is the code excerpt:

shanesObj.addTableEvents = function(){ table = document.getElementById("trackerTable"); for(i=1; i<table.getElementsByTagName("tr").length; i++){     row = table.getElementsByTagName("tr")[i];     orderID = row.getAttributeNode("id").value;     alert("before onclick: " + orderID);     row.onclick=function(){shanesObj.tableRowEvent(orderID);}; }}  shanesObj.tableRowEvent = function(orderID){ alert(orderID);} 

The table is located at the following location…

http://www.blackcanyonsoftware.com/OrderTracker/testAJAX.html

The id’s of each row in sequence are… 95, 96, 94…

For some reason, when shanesObj.tableRowEvent is called, the onclick is set up for all rows with the last value id that went through iteration on the loop (94).

I added some alerts to the page to illustrate the problem.

  • 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-11T03:40:34+00:00Added an answer on May 11, 2026 at 3:40 am

    When a function is called in javascript, the first thing that happens is that the interpreter sets the scope chain to the state it was in when the function was defined. In your case the scope chain looks like:

        GLOBAL      | CAll addTableEvents       | CALL onclick 

    So when the variable orderID is stumbled upon by the javascript interpreter it searches the scope chain for that variable. There is no orderID defined inside your onclick function, so the next spot to look is inside addTableEvents. You would think orderID is defined here, but since you did not declare orderID with the var keyword, orderID became a global variable, so orderID is not found inside addTableEvents. The last spot to look is inside GLOBAL, and sure enough it is there, and its value is the last one which it was assigned, and in this case its the last value of orderID = row.getAttributeNode(‘id’).value; in the for loop.

    To see this more clearly look at the following

    shanesObj.addTableEvents = function(){ table = document.getElementById('trackerTable'); for(i=1; i<table.getElementsByTagName('tr').length; i++){         row = table.getElementsByTagName('tr')[i];         orderID = row.getAttributeNode('id').value;         alert('before onclick: ' + orderID);         row.onclick=function(){var orderID = orderID; shanesObj.tableRowEvent(orderID);}; } orderID = -1; }  shanesObj.tableRowEvent = function(orderID){ alert(orderID); } 

    The result of a click here will always be -1.

    So, to get around this problem, I suggest you drop all the extra and unneeded code and use something like the following,

    for(i=1; i<table.getElementsByTagName('tr').length; i++){         row = table.getElementsByTagName('tr')[i];         row.onclick=function(){shanesObj.tableRowEvent(this.id);};  } 

    Just access the id attribute directly from the called object.

    P.S. I have no idea why your code doesn’t work in IE (it worked in my IE7).

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

Sidebar

Ask A Question

Stats

  • Questions 63k
  • Answers 63k
  • 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
  • added an answer Wouldn't that do? $(document).ready(function() { $('area').bind('click',function(e){ //popupcode }); //popupcode }); May 11, 2026 at 10:30 am
  • added an answer You can do this by applying a CATransform3D to your… May 11, 2026 at 10:30 am
  • added an answer Define and declare are similar but assign is very different.… May 11, 2026 at 10:30 am

Related Questions

I am having problems with my JScript code. I am trying to loop through
I am having a frequent problems with my web hosting (its shared) I am
I am having some performance problems with my Delphi 2006 app. Can you Suggest
I am having problems with implementing Observer pattern in my project. The project has
I am currently having some problems with <li> in IE6 essentially my code looks
I am having an odd problem with my SQL Server express 2005 DB. It
I am having a problem with binding values in my ActionScript components. I basically
I am having a problem getting my project to link with the PhysX libraries
I am having problems with text with multiple lines not reformatting properly when I
I am still having problems with figuring out how to create winforms in a

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.