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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:10:57+00:00 2026-06-14T10:10:57+00:00

I have the following code that adds an onmouseover event to a bullet onload

  • 0

I have the following code that adds an onmouseover event to a bullet onload

for (var i = 0; i <= 3; i++) {
    document.getElementById('menu').getElementsByTagName('li')[i].onmouseover = function () { addBarOnHover(i); };
}

This is the function that it is calling. It is supposed to add a css class to the menu item as the mouse goes over it.

function addBarOnHover(node) {
    document.getElementById('menu').getElementsByTagName('li')[node].className = "current_page_item"; }

When the function is called, I keep getting the error:

“document.getElementById(“menu”).getElementsByTagName(“li”)[node] is
undefined”

The thing that is stumping me is I added an alert(node) statement to the addBarOnHover function to see what the value of the parameter was. The alert said the value of the parameter being passed was 4. I’m not sure how this could happen with the loop I have set up.

Any help would be much appreciated.

  • 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-14T10:10:59+00:00Added an answer on June 14, 2026 at 10:10 am

    This is a common problem when you close over an iteration variable. Wrap the for body in an extra method to capture the value of the iteration variable:

    for (var i = 0; i <= 3; i++) {
      (function(i){ //here
        document.getElementById('menu').getElementsByTagName('li')[i].onmouseover = function () { addBarOnHover(i); };
      })(i); //here
    }
    

    an anonymous function is created each time the loop is entered, and it is passed the current value of the iteration variable. i inside the anonymous function refers to the argument of this function, rather than the i in the outer scope.

    You could also rename the inner variable for clarity:

    for(var i=0; i<=3; i++){
      (function(ii){
        //use ii as i
      })(i)
    }
    

    Without capturing the iteration variable, the value of i when it is finally used in the anonymous handler has been already changed to 4. There’s only one i in the outer scope, shared between all instances of the handler. If you capture the value by an anonymous function, then the argument to that function is used instead.

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

Sidebar

Related Questions

I have the following code that adds a record to my MySQL database via
i have the following code that adds data to unordered lists. The troublesome code
I have the following code that adds an observer in the loading of the
I have the following code that adds a file upload option to a div
I have a ugly piece of code that adds event handlers. The problem is,
I have the following code that adds a background worker into a VB.net WPF
I have built the following code with the Firefox add-on SDK that successfully adds
I have the following code that adds a button of type UIBarButtonItem on UIToolbar
I have added the following code to a php script that adds users to
I have following code that I am compiling in a .NET 4.0 project namespace

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.