tags:

views:

29

answers:

1

i have this code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Test jQuery</title>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">
$(function() {
$(".add").click(function() {
    $("#demos").append("<input type='text' size='20' name='txt[]'>  <a href='#' onClick='removeFormField(); return false;'>Remove</a>");

return false;
    });
});

    function removeFormField() {
    $(this).prev("input").remove();
    }


</script>

<body>
<a class="add" href="#">add</a>
<div id="demos"></div>


</body>
</html>

and when i press add its work good but when click remove its not remove anything

+1  A: 

You need to provide some element argument to your function:

function removeFormField() {
  $(this).prev("input").remove();
}

Currenlty, $(this) does not refer to anything. You should try something like this:

function removeFormField(elem) {
  elem.remove();
}
Sarfraz