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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:14:28+00:00 2026-06-05T21:14:28+00:00

I have this fiddle: http://jsfiddle.net/yub2B/4/ HTML: <input type=text /> <input type=text /> <input type=text

  • 0

I have this fiddle: http://jsfiddle.net/yub2B/4/

HTML:

<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />

JS:

function test(x)
{
    alert(x);
}

var text=document.getElementsByTagName("input");
for(var i=0,l=text.length;i<l;i++)
{
    text[i].addEventListener("keydown",function()
    {
        test(i);
    },false);
}

And instead of outputing 0 for first textbox, 1 for second textbox … it always ouput 4.

How can I fix this?

  • 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-05T21:14:29+00:00Added an answer on June 5, 2026 at 9:14 pm

    That’s infamous loop problem when assigning events inside loop. You are passing same value of i on each iteration, use this:

    for(var i=0,l=text.length;i<l;i++)
    {
       (function(i){
          text[i].addEventListener("keydown",function()
          {
             test(i);
          },false);
    
       })(i)
    }
    

    Updated Fiddle

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

Sidebar

Related Questions

I have created a fiddle to test this : http://jsfiddle.net/Ninjanoel/X6ShS/ which works fine :(
Given this little fiddle: http://jsfiddle.net/5Sw3h/8/ I have a navigation panel to the left, a
I have this Fiddle: http://jsfiddle.net/GSA5R/2/ Whereby i use the border property to create the
See this fiddle: http://jsfiddle.net/uqJHf/ I have set the first column to show up with
I have this fiddle which has an error --> http://jsfiddle.net/Osoascam/AkZZr/6/ (This is the version
I have the following javascript code found also in this fiddle: http://jsfiddle.net/periklis/k4u4c/ <button id
I have this fiddle: http://jsfiddle.net/y8Uju/5/ I am trying to save the numbers, because, when
I currently have this working fiddle - http://jsfiddle.net/B8Abd/ I would like to use jquerys
I made this fiddle http://jsfiddle.net/nAb6N/10/ As you can see I have 2 animators ,
I have this fiddle http://jsfiddle.net/jdsans/38GFS/ which I was trying to workout but I could

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.