title: jQuery click event reloads the page
body: <p>I have a jQuery code which runs on dom ready. It also creates elements dynamically. When I call the click event of one of these dynamic elements, instead of calling the code inside its click event, it reloads the page and the function on pageload is again called. Basically, it doesn't run the code inside click event at all. </p>
  
  <pre><code> $(document).ready(function(){
  
                  loadPage();
  
                  function loadPage() {   
  
                      var value = $("#search").val();
  
                      //create post data
                      var postData = { 
                          "value" : value
                      };
  
                      //define ajax config object
                      $.ajax({
                          type: "post",
                          url: "search.php",
                          data: postData, 
                          dataType: "json",
                          success: function(data) {
  
                              if(!data[0].success){
                                  alert(data[0].er);
  
                              }
                              else {
                                  $('#search-results').empty();
                                  var div_main = $("&lt;div&gt;").attr('id', "search-content").appendTo("#search-results"); 
  
                                  for (var x = 0; x &lt; data.length; x++) {
  
                                       var div = $("&lt;div&gt;").attr('id', "search-content-container").appendTo(div_main); 
  
                                       var div_en_quotes = $("&lt;div&gt;").attr('id', "search-en-quotes").html(data[x].cQuotes).appendTo(div);
                                       var div_author = $("&lt;div&gt;").attr('id', "search-author").html(data[x].vAuthor).appendTo(div);
                                       var div_ref = $("&lt;div&gt;").attr('id', "search-bookref").html("&lt;a class='bk_name' href='?bookname="+ data[x].vBookName +"'&gt;" + data[x].vBookName +"&lt;/a&gt; "+ data[x].vRef).appendTo(div);
  
  
                                  }
                              }
                          }
                      }); 
  
                  }
   });
  
  $(document).on("click", "a.bk_name", function(e){ 
  
                      e.preventDefault();
                      alert($('.bk_name').html());
  
              });
  </code></pre>
  
  <p>If I add <code>e.preventDefault</code>, it stops the page from reloading and does execute the code, but the value of the <code>anchor tag</code> remains static. That is, it only displays the first value in the loop even if I click on other values.</p>
  
  <p>I'm using jQuery 1.7.</p>