6

In a Lightning Component I have

<div style="border: green solid 3px;" onclick="console.log(123);">
    <br/>
    <br/>
    <br/>       
</div>

As far as I see, the old-school events do not work. What is the easiest way to get a click-event on a div?

2 Answers 2

9

Inline Javascript is not supported due to security threat and you can simply use onclick event and always follow the Unobtrusive principles.You can look at document here which mentions to avoid inline script

Try below code

 <div style="border: green solid 3px;" onclick="{!c.logData}">
   <br/>
   <br/>
   <br/>       
</div>

The controller code

({
   logData : function( cmp, evt,  h ){
     console.log(123);
   }
})
1

I believe the name of the event is onclick instead of click. I think you can bind your controller method to onclick by specifying a method.

Example: <div onclick='{!c.handle}' >Hello<div>

1
  • I fixed that, thanks. But my real issue is addressed by @MohithSrivastava
    – Uwe Heim
    Commented Dec 22, 2016 at 18:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .