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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:28:36+00:00 2026-05-18T08:28:36+00:00

I am creating a system where content is loaded dynamically from a JSON file.

  • 0

I am creating a system where content is loaded dynamically from a JSON file. The JSON file describes the content which is created by jQuery. I am trying to add events dynamically. It is working, but if I try to add events to two or more new dom elements, they all get whatever the last event was added. I guess I am doing something wrong with referencing to my new dom events, but can’t figure out what…

A partial example of JSON content would be:

{
  "container" : {
    "id" : "container",
    "dom_type" : "<div>",
    "attr" : {
      "class" : "container",
      "id" : "container"
    },
    "children" : {
      "input_submit" : {
         "dom_type" : "<button>",
         "html" : "Submit",
         "attr" : {
            "type" : "submit",
            "id" : "submit",
            "class" : "submit"
            },
         "event": {
            "type": "submit",
            "name": "submitevent"
            }
          },
        "input_cancel" : {
         "dom_type" : "<button>",
         "html" : "Cancel",
         "attr" : {
            "type" : "submit",
            "id" : "cancel",
            "class" : "submit"
            },
         "event": {
            "type": "submit",
            "name": "cancelevent"
            }
          }
        }
      }

Now, my function reads the JSON file, jquery makes it an object and with a series of if/then statements, I create some dom and insert it:

// here, json is a JavaScript object made by jQuery with getJson..
function makeDom (json, dom_container) {
     for (var child in json.children)
        {
            var tp = json.children[child];

            // make dom element and add id (id is required)
            if (!tp['attr'])
            {
                
                $.error('jquery.myplugin.loadscreen() - erreur: Required child object "attr" not found in ' + child + '.')
            }

            if (undefined == tp['attr']['id'])
            {
                
                $.error('jquery.myplugin.loadscreen() - erreur: Required parameter "id" not found in "attr" in ' + child + '.');
            }

// ---------------------------------------------------------------
// I guess there is something wrong with this 'el' variable, because
// when I add the event to it, all similar elements get the event
// ----------------------------------------------------------------
    //add ID
            var el = $(tp['dom_type']);
            el.attr('id', tp['attr']['id']);
            
            // add type if any
            if (tp['attr']['type']) {
                el.attr('type', tp['attr']['type'])
            }

            // add for  if any
            if (tp['attr']['for']) {
                el.attr('for', tp['attr']['for'])
            };
            
    // add class if any
    if (tp['attr']['class']) {
        el.addClass(tp['attr']['class']);
    }
                
            // add src if any
            if (tp['attr']['src']) {
                el.attr('src', tp['attr']['src'])
            };

            // add href if any
            if (tp['attr']['href']) {
                el.attr('href', tp['attr']['href'])
            };

            // add html if any
            if (tp['html']) {
                el.html(tp['html']);
            };
            
       // add value if any
    if (tp['attr']['value']) {
        el.attr('value', tp['attr']['value']);
    };
            
            // add event if any
            if (tp['event']) {
                el.bind(tp['event']['type'], function(e){
                    //do something;
             });    
    dom_container.append(el);           
}

then, what happens is that both buttons get the "cancel" event, as it was the last added… How can I get this ‘el’ variable to point the the right DOM element?

  • 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-18T08:28:37+00:00Added an answer on May 18, 2026 at 8:28 am

    The el variable is shared by all of the event handlers in a single closure.

    You need to make a separate method that attaches the event handler and takes the variables as parameters.
    Each call to the function will create a separate closure for the event handler, with a separate copy of the variable.

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

Sidebar

Related Questions

I'm creating Content management system in asp.net. But I need open source cascading stylesheet
I'm creating a website from scratch and I was really into this in the
If I create a dll called xaisoft.dll like this: Using System; Using System.Reflection; [assembly:AssemblyVersion(1.0.0.0)]
I'm throwing this out in case someone has encountered this before. When creating DesignData
I am sorry about the amount of code here. I have tried to show
I'm using an XmlSerializer to deserialize a particular type in mscorelib.dll XmlSerializer ser =
I read some articles about Application Domain.The deep reading finally resulted in whirling confusion.So
I wrote a class library in .NET 2.0 for database access some 2 years
I have a Linq object, and I want to make changes to it and
I have a varbinary field in my sql server database that needs to be

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.