this is my code…append is working but removing is not working. how to remove the appended div?
<html>
<head>
<script type="text/javascript">
function view(){
$('body').append('<div class="added"><p>something</p></div>');
};
function close(){
$('.added').remove();
} ;
</script>
</head>
<body>
<a onclick="view();">something</a>
<a onclick="close();">close</a>
</body>
</html>
Specify
window.closein your html handler:http://jsfiddle.net/ZTwd7/1/
Otherwise
close()refers to the nativeclosemethod, which doesn’t do anything.Here’s how you would emulate it with a “javascript” (as opposed to inline html attribute) handler:
This is not exact reproduction (nor does it work in IE etc, that’s not the point) but it would put the
.closeinWindow.prototypein front of the globalwindow.closein the scope, so it shadows it. You can still refer to it explicitly withwindow.close().You should also totally drop the above and use jQuery instead:
JS:
http://jsfiddle.net/ZTwd7/5/
Making an ajax request that fetches a html file to be appended: