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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:49:06+00:00 2026-06-18T03:49:06+00:00

Possible Duplicate: javascript closure not working as it should see the first code: var

  • 0

Possible Duplicate:
javascript closure not working as it should

see the first code:

var count = 0;
(function addLinks() {
    var count = 0; //this count var is increasing

    for (var i = 0, link; i < 5; i++) {
        link = document.createElement("a");
        link.innerHTML = "Link " + i;

        link.onclick = function () {
            count++;
            alert(count);//here all the paragraph updates the same variable
        };

        document.body.appendChild(link);
    }
})();

when the link gets clicked the counter variable keeps on increasing for each link element .This is an aspected reult

Second:

var count = 0;
$("p").each(function () {
    var $thisParagraph = $(this);
    var count = 0; //this count var is increasing too.so what is different between them .They both are declared within the scope in which closure was declared

    $thisParagraph.click(function () {
        count++;
        $thisParagraph.find("span").text('clicks: ' + count);
        $thisParagraph.toggleClass("highlight", count % 3 == 0);
    });
});

Here the closure function is not working as aspected.On each click on the paragraph element ,the counter var is increased but that increment is not displayed on click on second paragraph element?What is the reason behing this?Why is this happening?the count variable is not increasing for each paragraph element.At my previous question i didn’t got the satisfied answer so i requestioned

  • 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-18T03:49:07+00:00Added an answer on June 18, 2026 at 3:49 am

    The counter variable is declared inside the .each loop. Thus, each <p> has its own counter because the function is executed for each <p>. Any two paragraphs are not updating the same variable.

    In the first snippet, on the other hand, there is only one counter variable used (outside the loop), which is being updated by any click on any <p>. As a side note, note that declaring in inside the loop wouldn’t make a difference – you have to create a closure for each <p> separately to have each <p> have its own counter variable.

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

Sidebar

Related Questions

Possible Duplicate: JavaScript scope and closure What is this for? (function(){ //The code to
Possible Duplicate: Javascript Closure Problem In the following code, TrueThis.aChoices[i]['CallBack'] is a function when
Possible Duplicate: Javascript: closure of loop? I have following code inside javascript: for (var
Possible Duplicate JavaScript: Changing src-attribute of a embed-tag but this is not working for
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} AFAIK, there
Possible Duplicate: Javascript - array.contains(obj) What's wrong with this: var zipCodes =(['90001','90002','90003']); Test if
Possible Duplicate: JavaScript URL encode some scripts working fine started to give me this
Possible Duplicate: Javascript closure inside loops - simple practical example Calling setTimeout function within
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} There are

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.