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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:08:15+00:00 2026-05-13T07:08:15+00:00

I have a question relative to this code. It should (and it does) display

  • 0

I have a question relative to this code. It should (and it does) display a list of items, where you can add and remove entries by clicking on the plus and minus.

<html><head><script src="http://www.google.com/jsapi"></script>
<script>
google.load("jquery", "1.3.2");

app={};
app.attachList = function() {        
        return {
            _current_id : 0,
            addFilterAfter : function(after_entry_id) {
                that = this;
                new_id = this._newId();
                jQuery("#entry_"+after_entry_id).after("<li id=\"entry_"
                                +new_id+"\"><a href=\"javascript:void(0);\" id=\"entry_"
                                +new_id+"_add\">(+)</a><a href=\"javascript:void(0);\">(-)</a> hello "
                                +new_id+"</li>"); 
                jQuery("#entry_"+new_id+"_add")
                      .bind("click", function() { 
                                     that.addFilterAfter(new_id); 
                                    }  
                           );
            },  
            show : function() {
                that = this;
                new_id = this._newId();
                jQuery("#list").children().remove();
                jQuery("#list").append("<li id=\"entry_"
                         +new_id+"\"><a href=\"javascript:void(0);\" id=\"entry_"
                         +new_id+"_add\">(+)</a> hello </li>");
                jQuery("#entry_"+new_id+"_add")
                         .bind("click", function() { 
                             alert(new_id); // HERE
                             that.addFilterAfter(new_id); 
                             }
                         );
            },  
            _newId : function() {
                this._current_id++;
                return this._current_id;
            },
        };
}       

app.main = function() { l = app.attachList(); l.show(); }
google.setOnLoadCallback(function() {  jQuery.noConflict(); jQuery(document).ready(app.main); });  
</script>
<ul id="list" /></ul>
</head>
</html>

My question is relative to what happens at the line marked HERE. Ideally, if I click the first item +, I would expect this code to continue adding just below the first item. This does not happen. It continues incrementing the new_id (as you can see from the alert) instead of keeping the original new_id it was granted.

Now, I have a suspect about what’s going on, mostly involving the closure nature of javascript, but I want to hear it explained from someone more skilled.

As a side note, if I partition the logic for binding the event in a separate method, it works as expected… from this my suspect.

  • 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-13T07:08:16+00:00Added an answer on May 13, 2026 at 7:08 am

    The problem that is causing this behavior in your code is that you’re missing the var statement when you assign a value to new_id.

    In JavaScript when you make an assignment to an undeclared identifier (an identifier not reachable in the scope chain), it becomes global (window.new_id is created) and that’s why it keeps incrementing.

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

Sidebar

Ask A Question

Stats

  • Questions 267k
  • Answers 267k
  • 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
  • Editorial Team
    Editorial Team added an answer First, there's no such thing as an ASCII code above… May 13, 2026 at 12:49 pm
  • Editorial Team
    Editorial Team added an answer When you create the new image, give it the mode… May 13, 2026 at 12:49 pm
  • Editorial Team
    Editorial Team added an answer In article you see that "feature" file contains ControlAssembly="SharePointSolutionPack, Version=1.0.0.0,… May 13, 2026 at 12:49 pm

Related Questions

I have a python module which generates large data files which I want to
I have a table where one column has duplicate records but other columns are
I have some markup for a popup menu that works in firefox but not
I am struggling with something that I think should be easily (ish). I have
I'm creating a simple set of tables to store a number of lists. The

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.