javascript - Trying to get $(document).on('Click') to work for a p tag -
i trying document.on function work when user clicks on p tag has class called card. far function non responsive. should change function respond when click on p tag has class called card. here html , jquery code.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="description" content="contacts"> <title>contacts</title> <link rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $('button').click(function(){ var first = $('input.first').val(); var last = $('input.last').val(); var desc = $('textarea').val(); $('div.right').append("<p class='card'><h1>"+ first +' '+last+"</h1><h4>'click description!'</h4></p><p class='back'>"+desc+"</p>"); return false; }); $(document).on('click', 'p.card', function(){ //this function trying work. alert("test"); }); }); </script> </head> <body> <div class="wrapper"> <div class="left"> <form action="#"> <p >first name: <input type="text" name="fname" class="first"></p> <p >last name: <input type="text" name="lname" class="last"></p> <p class="desc">description:</p> <p><textarea rows="4" cols="50"></textarea></p> <button>add user</button> </form> </div> <div class="right"> </div> </div> </body>
here css code
*{ /* outline: 2px dotted red;*/ border: 0 none; padding: 0; margin: 0; } div.wrapper{ width: 970px; min-height: 100px; margin-left: auto; margin-right: auto; } div.left{ border: 2px solid black; width: 500px; display: inline-block; } div.right{ width: 200px; display: inline-block; height: 100px; vertical-align: top; padding-right: 100px; text-align: center; } p{ margin-left: 80px; margin-top: 20px; font-size: 20px; width: 400px; } p.email{ margin-left: 45px; } button{ margin: 30px; height: 20px; width: 100px; margin-left: 75px; text-align: center; } div.card{ margin-left: 100px; margin-bottom: 20px; border: 2px solid black; width: 300px; height: 100px; text-align: center; } p.back{ display: none; margin: 0px; padding: 0px; text-align: center; } textarea{ border: 2px solid black; }
somehow p.class
appending broken, below other elements , not being identified sender jquery event.
the following works fine:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="description" content="contacts"> <title>contacts</title> <link rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $('button').click(function(){ var first = $('input.first').val(); var last = $('input.last').val(); var desc = $('textarea').val(); var p = $('<p class="card">'); p.append("<h1>"+ first +' '+last+"</h1><h4>'click description!'</h4><p class='back'>"+desc+"</p>"); $('div.right').append(p); return false; }); $(document).on('click', 'p.card', function(){ //this function trying work. alert("test"); }); }); </script> </head> <body> <div class="wrapper"> <div class="left"> <form action="#"> <p >first name: <input type="text" name="fname" class="first"></p> <p >last name: <input type="text" name="lname" class="last"></p> <p class="desc">description:</p> <p><textarea rows="4" cols="50"></textarea></p> <button>add user</button> </form> </div> <div class="right"> </div> </div> </body>
Comments
Post a Comment