title: Javascript: Calling nested function from dynamicaly created instance
body: <p>I am trying to create multiple instance of same function, but wants to preserve their own properties. so that when the button created in 1st instance, the onclick event of that should be pointed to the function from same 1st instance. but (this) keyword not working in this case. i know something else need to be done.</p>
  
  <p>JS:</p>
  
  <pre><code>function updateAlias(mac, realName, displayName) {
  
    var prevHtml = jQuery("#" + mac).html();
  
    var formHtml = "&lt;input type='text' id='alias-" + mac + "' value='" + jQuery.trim(displayName) + "' /&gt;";
    formHtml += "&lt;button class='btn btn-primary btn-xs' onclick='" + this.apply() + ";'&gt;Apply&lt;/button&gt;";
  
    jQuery("#" + mac).html(formHtml);
  
    var apply = function() {
      alert(mac);
    };
  }
  </code></pre>
  
  <p>HTML:</p>
  
  <pre><code>&lt;div id="id1"&gt;
    &lt;div onclick="new updateAlias('id1','android-8112ad950b0bca36','device 1')"&gt;Device 1&lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div id="id2"&gt;
    &lt;div onclick="new updateAlias('id2','android-8112ad950b0bca36','device 2')"&gt;Device 2&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>JS Fiddle: <a href="https://jsfiddle.net/amitshahc/zg74v4k3/9/" rel="nofollow">https://jsfiddle.net/amitshahc/zg74v4k3/9/</a></p>