Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Issues</h1> <p>I'm not sure if it was just through your abbreviation when copying to SO but none of the code you have given would work "as pasted":</p> <ul> <li>You have no id's on the buttons so binding to the id (I assume this is just because they weren't relevant in the first paste!)</li> <li>The second paste uses the jQuery library and so you would need to make sure jQuery is included and allowed: <a href="https://stackoverflow.com/a/10928761/969807">https://stackoverflow.com/a/10928761/969807</a></li> <li>In the third paste, the second parameter of addEventListener should be a function accepting the event as the first and only parameter (if wanted). (Read On)</li> </ul> <h1>Solution</h1> <p>The most common way to bind an event in the scenario you describe would be like so:</p> <pre><code>document.getElementById('btn1').addEventListener('click', function (){ foo(1, 2); }, false); </code></pre> <p>&nbsp;</p> <p>However if there were a lot (or a variable amount) of buttons, I would probably do it like so:</p> <p>popup.html:</p> <pre><code>&lt;div id="buttons"&gt; &lt;button data-param1="1" data-param2="2"&gt;Button 1&lt;/button&gt; &lt;button data-param1="2" data-param2="3"&gt;Button 2&lt;/button&gt; &lt;/div&gt; </code></pre> <p>popup.js:</p> <pre><code>var els = document.querySelectorAll('#buttons button'), i, total, param1, param2; for (i = 0, total = els.length; i &lt; total; i++) { el = els[i]; param1 = parseInt(el.getAttribute('data-param1')); param2 = parseInt(el.getAttribute('data-param2')); el.addEventListener('click', function (){ foo(param1, param2) }, false); } </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. 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