title: How to use JQuery selectors from dynamic Elements created from.Append function?
body: <p>I have this loop creating several divs with elements in it. I want to be able to attach a click event to every link with the "linky" class, and this link would read the contentID attribute.</p>
  
  <p>I've searching forever, find cases to use selectors in dynamic created elements but just can't apply to my case. Any tips?</p>
  
  <pre><code>                    for (i = 0; i &lt; msg.length; i++) {
                          var htmlCode = "&lt;a href='/Controller/Details/" + msg[i].ID + "' style = 'float: left;'&gt;&lt;img class='packageImage' border='0' src='" + msg[i].Size0Path + "' /&gt;&lt;/a&gt;";
                          htmlCode += "&lt;span style='float: left;margin: 5px 0px 0px 10px;'&gt;" + "&lt;b&gt;" + msg[i].TheName + "&lt;/b&gt;&lt;br&gt;";
  
  
                          if (msg[i].IsPaused == true) {
  
                              htmlCode += "&lt;a href='#bar' class=linky contentID='" + msg[i].ID + "'&gt;Resume&lt;/a&gt;";
                          } else {
  
                              htmlCode += "&lt;a href='#bar' class=linky contentID='" + msg[i].ID + "'&gt;Pause&lt;/a&gt;";
                          }
                          htmlCode += "&lt;/span&gt;";
                          htmlCode += "&lt;div class='clear'&gt;&lt;/div&gt;";
                          $("#divContent").append(htmlCode);
                      }
  </code></pre>
  
  <p>Given the answers, I'm trying to attach the event to class linky, but I just not sure where, see more details below, my instructions are creating the dynamic Elements from the result of ajax submit(post). </p>
  
  <pre><code>                    success: function (msg) {
  
                          $("body").on("click", "a.linky", function () {
                              alert($(this).attr("contentID"));
                          });
  
  
  
   for (i = 0; i &lt; msg.length; i++) {
                              var htmlCode = "&lt;a href='/Controller/Details/" + msg[i].ID + "' style = 'float: left;'&gt;&lt;img class='packageImage' border='0' src='" + msg[i].Size0Path + "' /&gt;&lt;/a&gt;";
                              htmlCode += "&lt;span style='float: left;margin: 5px 0px 0px 10px;'&gt;" + "&lt;b&gt;" + msg[i].TheName + "&lt;/b&gt;&lt;br&gt;";
  
  
                              if (msg[i].IsPaused == true) {
  
                                  htmlCode += "&lt;a href='#bar' class=linky contentID='" + msg[i].ID + "'&gt;Resume&lt;/a&gt;";
                              } else {
  
                                  htmlCode += "&lt;a href='#bar' class=linky contentID='" + msg[i].ID + "'&gt;Pause&lt;/a&gt;";
                              }
                              htmlCode += "&lt;/span&gt;";
                              htmlCode += "&lt;div class='clear'&gt;&lt;/div&gt;";
                              $("#divContent").append(htmlCode);
                          }
  
  
   }
  </code></pre>