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}

  • 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 – Eric Apr 19 '17 at 15:01
  • thanks for response . event.target is working fine when I create the static checkbox instead of dynamic – Jitendra Apr 20 '17 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. – TrevorBliss Apr 20 '17 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. – TrevorBliss Apr 20 '17 at 17:15
2

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

handlePress : function(component) { ... }

to

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

  • thanks for response . but changing handlePress : function(component) { ... } to handlePress : function(component, event) { ... } method is not getting called – Jitendra Apr 20 '17 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 – Eric Apr 20 '17 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.