3

I am creating dynamic checkbox component in Lightning in helper class but I am not able to capture click event in controller class as value is undefined for event.

Helper

 var checkBoxDef = [];
 var checkBoxCmps = [];

 checkBoxDef.push([

                "ui:inputCheckbox",

                    {
                       "class" : "design-checkbox",
                        "id" : "accordion-c-"+i,
                        "aura:id" : "accordion-c-"+i, 
                         "click": component.getReference("c.handlePress")
                    }

            ]);

 $A.createComponents(
        checkBoxDef,
        function(cmps, status, errorMessage){
            if (status === "SUCCESS") {
                for (var i = 0; i < cmps.length; i++) {
                    var checkBoxCmp = cmps[i];              
                    checkBoxCmps.push(checkBoxCmp);
                }
            }
            else if (status === "INCOMPLETE") {
                console.log("No response from server or client is offline.")
            }
            else if (status === "ERROR") {
                console.log("Error: " + errorMessage);
                }
            }
        ); 

Controller

handlePress : function(component) {
        var sectionId;
         console.log(component);

       if (event.target != undefined) {
           sectionId = event.target.id;
        } else {
            sectionId = event.getSource().getLocalId();
             }
         }   

Error

This page has an error. You might just need to refresh it. Action failed: c:CCustomLightningPage$controller$handlePress [Cannot read property 'target' of undefined] Failing descriptor: {c:CCustomLightningPage$controller$handlePress}

4
  • 1
    I believe this has been a common problem with Locker service but I could also be missing something else that could be causing it. I know they were working on the event.target issue with the lightning namespace. Maybe @DougChasman could chime in Commented Apr 19, 2017 at 15:01
  • thanks for response . event.target is working fine when I create the static checkbox instead of dynamic Commented Apr 20, 2017 at 6:33
  • @Jitendra I've answered this specific question below. If you've made the event parameter change and see a new issue, please open a new question with the full repro (.cmp, helper, controller file) and the new error or what things you have tried and what is not working as you'd expect. Commented Apr 20, 2017 at 17:14
  • Also, please only tag the question with "locker-service" if you think it's actually related to LockerService. From this question I don't see anything that would indicate that. Commented Apr 20, 2017 at 17:15

1 Answer 1

2

You need to declare the event parameter in your controller function. Change the following

handlePress : function(component) { ... }

to

handlePress : function(component, event) { ... }

2
  • thanks for response . but changing handlePress : function(component) { ... } to handlePress : function(component, event) { ... } method is not getting called Commented Apr 20, 2017 at 6:26
  • 2
    @Jitendra Trevor is right. Event is undefined in that method. If it is not getting called that is a separate issue Commented Apr 20, 2017 at 12:42

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.