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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:11:20+00:00 2026-05-22T12:11:20+00:00

The title may not make sense and nor would a description alone, so here’s

  • 0

The title may not make sense and nor would a description alone, so here’s a sample of the code:

for(var a=0;a<10;a++) {
    var b=document.createElement('b')
    b.onclick=function() {
        alert(a)
    }
    b.innerHTML=a
    document.body.appendChild(b)
}

What’s odd is that when I set innerHTML to a, it uses the current value of a. So, this code creates ten <b> elements with the values 0, 1, 2, 3, 4, 5, 6, 7, 8, & 9. This works perfectly. What doesn’t, however, is the onclick function. It returns the final value of a (10) when any of these elements is clicked. I’ve tried setting another variable to a and then using that other variable in the onclick event, but still it returns the final value of a. How would I make it use the value of a when onclick is set?

  • 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-05-22T12:11:21+00:00Added an answer on May 22, 2026 at 12:11 pm

    Try the following

    b.onclick= function(arg) {
        return function() {
            alert(arg);
        }
    }(a);
    

    The problem you were hitting is that javascript doesn’t use block scope. Instead all variables have a lifetime of their containing function. So there was only ever one a captured in all of the onclick functions and they all see it’s final value.

    The solution works around this by creating a new function and value per scope and then immediately setting that value to the current value of a.

    Here’s a version that’s a bit expanded and perhaps a bit more readable

    var createClickHandler = function(arg) {
      return function() { alert(arg); };
    }
    
    for(var a=0;a<10;a++) {
        var b=document.createElement('b')
        b.onclick = createClickHandler(a);
        b.innerHTML=a
        document.body.appendChild(b)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I apologize that the title may not make sense, so let me describe my
Title may be confusing, but here's explanation with code. Based on some conditions, I
ok not as simple as title may make it sound. I tried this in
First time using CSS so my question may not make sense. I am using
Ok so the title may have been confusing so i have posted 2 code
(The title may not be proper but I try my best to phrase it)
This is not duplicate, as it may seam from the title. Continue reading. So
The title may not really explain what I'm really trying to get at, couldn't
Ok Title may not be clear so let me explain. I have 2 classes,
The title may not be very appropriate, so apologies in advance. I'm building a

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.