w3resource

jQuery: Click on a paragraph and add another paragraph

jQuery Fundamental - II : Exercise-75

Click on a paragraph and add another paragraph.

Sample solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Click on a paragraph and add another paragraph.</title>
</head>
<body>
<p>Click me to add new paragraph.</p>
</body>
</html>

CSS Code :

 p {
 background: orange;
 font-weight: bold;
 cursor: pointer;
 padding: 5px;
 }
 

JavaScript Code :

$( "body" ).delegate( "p", "click", function() {
$( this ).after( "<p>New paragraph!</p>" );
});

See the Pen jquery-fundamental-exercise-75 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Animate the hiding and showing of two divs, delaying the first before showing it.
Next: End a custom queue function using dequeue which allows the queue to keep going.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/jquery-exercises/2/jquery-fundamental-exercise-75.php