title: jquery: add event to newly created element
body: <p>I have added a new element via jquery. Now I want a function to trigger when the element is clicked. I know there are many answers to this, but they don't seem to work for me. Not sure what I am doing wrong here.</p>
  
  <p>I have tried:</p>
  
  <pre><code>new_ele = "&lt;a&gt;click me&lt;/a&gt;"
  new_ele.click(function() {alert('xxxx');}); 
  other_ele.append(new_ele);
  </code></pre>
  
  <p>This fails before the new_ele is appended.</p>
  
  <p>I have also tried (instead of the second line above)</p>
  
  <pre><code>new_ele.onclick = function() { alert('blah'); };
  </code></pre>
  
  <p>This appends the element, but nothing happens when I click.</p>
  
  <p>And</p>
  
  <pre><code>new_ele.on('click',function() {alert('ddd');});
  </code></pre>
  
  <p>This also fails before the element is appended.</p>