Question

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 2 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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top