Search code examples
javascripthtmlbuttondojorowdeleting

domConstruct place button not firing


I am creating table rows dynamically using dojo domConstruct. One of the column contains a button for delete function.But i dont know how to connect onclick event for the delete button. Below is my js code for creating the row.

domConstruct.place("<tr class='test'><td>" +
    " Account name"+ XXXX+" "+" Account number is is $" + data1 +
    "</td><td><input type ='button' onclick='deleteFunction(this);' value='remove' id=" +
    buttonId + "/></td></tr>","tradeInValue","");

So now how i connect it to

on(dom.byId("buttonId"),"click",function(){

// my code goes in here


});

I have no clue here. Basically i need to remove the row from a table on click of a button. I am using dojo in javascript file.

Updated.

o

n(dom.byId("submitButton"), "click", function(evt){

        var name=registry.byId("name").get('value');
        var detail = registry.byId("manufacturer").get('value');
        var id=registry.byId("model").get('value');
        var make=registry.byId("shaft").get('value');

            var xhrArgs={
                    url:"./name/getValue",
                    content:{name:name,detail:detail,id:id,make:make},
                    handleAs:"json",
                    load:function(data){

                        var data1=data/100;
                        var row=domConstruct.create("tr",null,"tradeInValue");
                        domConstruct.create("td",{innerHTML:" Name
"+ detail+" "+id+"  Value is $"+data1},row);
                        var actions=domConstruct.create("td",null,row);
                        var btn=domConstruct.create("input",{
                            id:idRow,
                            type:"button",
                            value:"Remove"
                        },actions);

                        btn.addEventListener("click", function(evt) {
                              console.log("Deleting");
                              console.log(evt.target.parentNode.parentNode.idRow);
                              domConstruct.destroy(evt.target.parentNode.parentNode);
                            });

                        var test={ 
                                "name" : name,
                                "detail"  : detail,
                                "id" :id,
                                "tradePrice" :data,
                                "make":make

                        };

                        tradeDetails.clubDetails.push(test);

                        }
                    }



            var deferred=dojo.xhrPost(xhrArgs);

        }
    });

Solution

  • The easiest way is to create your DOM nodes individually, for example:

    var row = domConstruct.create("tr", null, "myData");
    domConstruct.create("td", { innerHTML: person.id }, row);
    domConstruct.create("td", { innerHTML: person.name }, row);
    var actions = domConstruct.create("td", null, row);
    var btn = domConstruct.create("input", {
      type: "button",
      value: "Remove"
    }, actions);
    

    This allows you to easily attach event handlers to btn, while still preserving context and still having access to the data you're working with, for example:

    btn.addEventListener("click", function() {
      console.log("Deleting", person);  
    });
    

    To delete the row itself you could use the Event.target property, which gives you access to the button node itself. If you use the Node.parentNode property, you can eventually access the row itself, and then you can remove it using dojo/dom-construct::destroy()

    For example:

    btn.addEventListener("click", function(evt) {
      domConstruct.destroy(evt.target.parentNode.parentNode);
    });
    

    A full example can be found on JSFiddle.