Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way you are doing it is fine, but your event listener for the <code>click</code> event should be like this:</p> <pre><code>button.addEventListener("click", function() { alert("alert");}); </code></pre> <p>Notice, the <code>click</code> event should be attached with <code>"click"</code>, not <code>"onclick"</code>.</p> <p>You can also try doing this the old way:</p> <pre><code>function onload() { var button = document.getElementById("buttonid"); // add onclick event button.onclick = function() { alert("alert"); } } </code></pre> <h3>Update 1</h3> <p>You need to also monitor for IE &lt; 9, because those Vs use <code>attachEvent()</code>. Attach the event like this, so it will work with dinosaur browsers:</p> <pre><code>if(button.addEventListener){ button.addEventListener('click', function() { alert("alert");}); } else if(button.attachEvent){ // IE &lt; 9 :( button.attachEvent('onclick', function() { alert("alert");}); } </code></pre> <h3>Update 2</h3> <p>Based on your edit, this <strike>should work</strike> <a href="http://jsfiddle.net/Shef/GcU4Y/" rel="noreferrer">works just fine</a>.</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; function init() { var button = document.getElementById("buttonid"); if(button.addEventListener){ button.addEventListener("click", function() { alert("alert");}, false); } else if(button.attachEvent){ button.attachEvent("onclick", function() { alert("alert");}); } }; if(window.addEventListener){ window.addEventListener("load", init, false); } else if(window.attachEvent){ window.attachEvent("onload", init); } else{ document.addEventListener("load", init, false); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input type="button" id="buttonid" value="click"&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Please, do not use <code>window.onload = on_load();</code>, this will prevent all other <code>onload</code> event listeners from getting fired, or you are risking for your event listener to get overwritten. Consider attaching the <code>onload</code> event the way I am suggesting above.</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.
    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