Link to home
Start Free TrialLog in
Avatar of Nathan Riley
Nathan RileyFlag for United States of America

asked on

Remove Dynamic Input Box

http://jsfiddle.net/ChrisStanyon/8ZYuF/

How can I add some text to the right of the box that says "remove" and on click removes the added box?
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just try out this piece of code.I have used it.hope this is your exact requirement.
Copy paste the code and try it out.


<html>
<head>
<script  src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>   
<script type="text/javascript">
$(function() {


$('#addNew').live('click', function() {
	var i = jQuery('#test tr').length + 1;
                	
                	
	 	$('#addinput').append('<table id="test" name="test" width=100%><tr class="rows"><td><input type="text"  id="p_name_'+i+'_1" name="p_name[]" value=""></td><td><input type="text"  id="p_url_'+i+'_2" name="p_url[]" value=""></td><td><a href="#" id="reminvoiceNew">Remove</a> </td></tr></table>');

i++;
                	
return false;
});

jQuery('#reminvoiceNew').live('click', function() {
    	
    jQuery(this).parent().parent().remove();

return false;
});
});

</script>

</head>
<body>
<h2>Add another set</h2>

<div id="addinput">
<p>
Product: <input type="text" id="p_name" size="20" name="p_name" value="" placeholder="Input Value" />
Price: <input type="text" id="p_url" size="20" name="p_url" value="" placeholder="Input Value" />


<a href="#" id="addNew">Add</a>
</p>
</div>

</body>
</html>

Open in new window



Or Look at this this fiddle similar add and remove without using live
http://jsfiddle.net/eqVWz/1/
@ramyajanarthanan - your code will create duplicate IDs on the table and the remove button (the remove function on the button won't work). You don't need IDs on the inputs. You're also using an old version of jQuery. In the current version, the live() function is deprecated. $i++ is not needed (it's destroyed each time the function is run)