Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>myhandle.onclick = myfunction(param1,param2);</p> </blockquote> <p>This is a common beginner error at a JavaScript level, and not something that libraries can really fix for you.</p> <p>You are not assigning <code>myfunction</code> as a click handler, you are <strong>calling</strong> <code>myfunction</code>, with param1 and param2 as arguments, and assigning the return value of the function to <code>onclick</code>. <code>myfunction</code> doesn't return anything, so you'd be assigning <code>undefined</code> to <code>onclick</code>, which would have no effect..</p> <p>What you mean is to assign a reference to <code>myfunction</code> itself:</p> <pre><code>myhandle.onclick= myfunction; </code></pre> <p>To pass the function some extra arguments you have to make a closure containing their values, which is typically done with an anonymous inline function. It can take care of passing the event on too if you need (though either way you need a backup plan for IE where the event object isn't passed as an argument):</p> <pre><code>myhandle.onclick= function(event) { myfunction(param1, param2, event); }; </code></pre> <p>In ECMAScript Fifth Edition, which will be the future version of JavaScript, you can write this even more easily:</p> <pre><code>myhandle.onclick= myfunction.bind(window, param1, param2); </code></pre> <p>(with <code>window</code> being a dummy value for <code>this</code> which you won't need in this case.)</p> <p>However, since many of today's browsers do not support Fifth Edition, if you want to use this method you have to provide an implementation for older browsers. Some libraries do include one already; here is another standalone one.</p> <pre><code>if (!('bind' in Function.prototype)) { Function.prototype.bind= function(owner) { var that= this; var args= Array.prototype.slice.call(arguments, 1); return function() { return that.apply(owner, args.length===0? arguments : arguments.length===0? args : args.concat(Array.prototype.slice.call(arguments, 0)) ); }; }; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload