Note that there are some explanatory texts on larger screens.

plurals
  1. PORuntime function declaration on event handler with variables is executing the handlers function
    primarykey
    data
    text
    <p>I have a function which adds some rows to a table and at the end there is an expander. When the page is first rendered, there are 30 rows and the expanders are triggered by mouse clicks as the <code>onclick();</code> events are described inline in the document markup.</p> <p>When I add the new <code>onclick();</code> I am doing so like this in a loop to load the next 30 objects via some JSON I get through XHR.</p> <pre><code>newCell.onclick = (function(id){toggleCertificate(id, this, i);})(currCert['id']); </code></pre> <p>now <code>currCert</code> contains an integer <code>'id'</code> I want to be in this handler so that for each <code>i</code>, there will be a different integer in place of <code>currCert['id']</code> which is what <code>toggleCertificate(integer id ...);</code> expects</p> <pre><code>newCell.onclick = function(){toggleCertificate(12983, this, 5);}; </code></pre> <p><code>this</code> needs to be passed down as if it refers to the <code>newCell</code> object and the other two variables need to become passed down as integers to be interpreted when <code>toggleCertificate</code> is executed as their values</p> <p>What am I doing wrong? I find this syntax quite tricky surrounding attaching functions to event handlers.</p> <p>Currently this code behaves as trying to execute "toggleCertificate" with data I cannot debug. Firebug's DOM inspecter reveals the attached onclick becomes</p> <pre><code>(function (){toggleCertificate(currCert['id']), this, i}) </code></pre> <p>And the page begins executing all the correct <code>onclick</code> functions without being clicked and simultaneously.</p> <p>I want the onclick event to equal this string</p> <pre><code>"toggleCertificate(" + currCert['id'] + ", this, " + i + ");" </code></pre> <p>Sorry if any words I have used not proper JavaScript standards especially concerning "event handlers", "object" and "inline" -_-</p>
    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.
 

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