title: onclick function executes without any click on JavaScript
body: <p>I am working with some JavaScript and Ajax functions. I am going to put some code while I explain myself to be more clear.</p>
  <p>I have this element: <code>&lt;div id=&quot;divTest&quot; onclick=&quot;test()&quot;&gt;YES&lt;/div&gt;</code></p>
  <p>When the user clicks the DIV, the function replaces the &quot;YES&quot; for something like this:</p>
  <pre><code>YES &lt;input type=&quot;radio&quot; name=&quot;testing&quot; value=&quot;YES&quot; onclick=&quot;test_2(this.value)&quot; checked /&gt; 
  NO &lt;input type=&quot;radio&quot; name=&quot;testing&quot; value=&quot;NO&quot; onclick=&quot;test_2(this.value)&quot; /&gt;
  </code></pre>
  <p>This allows the user to select again the right option. Then I want to replace the radiobuttons with new value, so when the user selects any option, the DIV replaces the &quot;radio&quot; options and displays the value selected (YES OR NO) as it was on the beginning.</p>
  <p>At this point everything works perfect.</p>
  <p>Here is my problem:</p>
  <p>I want the DIV to have the <code>onclick()</code> as the beginning <code>onclick=&quot;test()&quot;</code> so I do it from JavaScript giving it the property this way: <code>div.onclick = function() { test();};</code>. The function <code>test()</code> is executed even if the user does not click on the div. It executes both function right away and does not wait until there is a click on it.</p>
  <p>Does anyone knows how can I make the function to wait until there is any click on it? Am I giving the <code>onclick</code> property incorrectly?</p>
  <p>I hope I made myself clear.</p>
  <p>Here are the functions:</p>
  <pre><code>function test() {
     var div = document.getElementById(&quot;divTest&quot;);
     //I DO NOT INCLUDE THE AJAX CODE BUT THE RESPONSE INCLUDE THE RADIO BUTTONS METIONED ABOVE
     div.innerHTML = xml.responseText();
  }
  
  function test_2(newValue) {
     div = document.getElementById(&quot;newValue&quot;);
     div.innerHTML = newValue;
     div.onclick = function() { test(); };
  }
  </code></pre>