Home > other >  Javascript define a function within event handler
Javascript define a function within event handler

Time:11-15

How can I write a function within the onclick event of a button

What I want to do is something similar to this:

<button onclick="(function(){
     console.log('some code here')
 })">My button</button>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You'll need to invoke the function:

<button onclick="(function(){
                     console.log('click')
                 })()">My button</button>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

<button>
onclick="(function(){console.log('click')})()"
>Button 
</button>
  • Related