html - Anchor tag with jquery not working -


calling jquery dynamically generated anchor tag not working. whereas same hard coded anchor tag jquery working fine.

code:

<!doctype html> <html> <head>     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">      </script>     <script>     $(document).ready(function(){          var value = "hi!!!";         $("button").click(function(){             $("#box").html("<a class='dynamic' href='#' dataid='"+value +"'>generate</a>");         });         $(".hardcode").click(function () {             alert("calling function");         });         $(".dynamic").click(function () {             alert("ggsss function");         });      });     </script> </head> <body>     <a class="hardcode" href="#" dataid="sss">generate</a>     <button>change content of p elements</button>      <div id="box">     </div> </body> </html> 

use $(document).on("click") dynamically generated element

var value = "hi!!!";  $("button").click(function(){      $("#box").html("<a class='dynamic' href='#' dataid='"+value +"'>generate</a>");  });  $(document).on("click",".hardcode",function () {   alert("calling function");  });  $(document).on("click",".dynamic",function () {   alert("ggsss function");  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>   <a class="hardcode" href="#" dataid="sss">generate</a>       <button>change content of p elements</button>         <div id="box">       </div>


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -