Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to get $(document).on('Click') to work for a p tag

I am trying to get my document.on function to work when the user clicks on the p tag that has a class called card. So far the function is non responsive. What should I change so the function will respond when I click on the p tag that has a class called card. Here is my HTML and 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 for description!'</h4></p><p class='back'>"+desc+"</p>");

            return false; 
        });

        $(document).on('click', 'p.card', function(){ //this is the function I am trying to get to 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 is my 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;
}
like image 468
Aaron Avatar asked Dec 22 '16 04:12

Aaron


People also ask

Why on click is not working jQuery?

So Why Does It Happen? JQuery OnClick Method is bound to an element or selector on page ready/load. Therefore if that element you want to click isn't there at the time of page ready, the binding can't happen.

How do you get the element that was clicked?

To get the clicked element, use target property on the event object. Use the id property on the event. target object to get an ID of the clicked element.

Does a tag support onclick?

All HTML elements can have an onclick attribute. See the HTML 5 specification for confirmation.


2 Answers

Somehow the p.class you were appending was broken, it was below the other elements and was not being identified as the sender by 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 for description!'</h4><p class='back'>"+desc+"</p>");
                $('div.right').append(p);
                return false;
            });
          
            $(document).on('click', 'p.card', function(){ //this is the function I am trying to get to 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>
like image 103
Piou Avatar answered Oct 24 '22 09:10

Piou


your code were not right , the appending elements not formatted as your wish , thats why the click function not working, check the below snippt i have fixed your code, hope fully this will help you

$(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"></p>');
			$('.card').html("<h1> first +' '+last</h1><h4>Click for description!'</h4><p class='back'>+desc+</p>"
		);
			
	});

	$(document).on('click', 'p.card', function(){ //this is the function I am trying to get to work.  
		alert("test");

	});
});
* {
	/*  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;
}
<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>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
like image 42
Jishnu V S Avatar answered Oct 24 '22 11:10

Jishnu V S