• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 685
  • Last Modified:

Delete appended div with jquery

Hello,

I am trying to delete a div after it was dynamically loaded by jquery but it does not remove it.

Please see my code, any guesses on how to delete it?
<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>
<div class="ap">some text</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
	$(".ap").click(function(){
		$(".ap").after("<div><div class='delete'>x</div>aaaa</div>");
	});
	$(".delete").click(
	function(){
		$(this).parent().remove();
		}
	);
</script>

</body>

</html>

Open in new window

0
degaray
Asked:
degaray
1 Solution
 
leakim971PluritechnicianCommented:
use : http://api.jquery.com/live/
<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>
<div class="ap">some text</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
	$(".ap").click(function(){
		$(".ap").after("<div><div class='delete'>x</div>aaaa</div>");
	});
	$(".delete").live("click",
	function(){
		$(this).parent().remove();
		}
	);
</script>

</body>

</html>

Open in new window


or :

<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>
<div class="ap">some text</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
	$(".ap").click(function(){
		$(".ap").after("<div><div class='delete'>x</div>aaaa</div>");
	$(".delete").click(
	function(){
		$(this).parent().remove();
		}
	);

	});
</script>

</body>

</html>

Open in new window

0
 
degarayAuthor Commented:
Thanks a lot!!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now