Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Remove Dynamic Input Box

Posted on 2013-10-24
3
Medium Priority
?
422 Views
Last Modified: 2013-10-25
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?
0
Comment
Question by:Nathan Riley
3 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 39599398
$("#add").click(function(){
    var name = 'textbox[*]'.replace('*', $('.newRow').length + 1);
    $('<input>')
        .attr({'type':'text', 'name': name})
        .val('new')
        .addClass('newRow')
        .insertBefore("button#add")
        .after('<input type="button" class="remove" value="Remove"><br/>');
});
$(document).on("click",".remove",function(){
  $(this).prev().remove() // input
  $(this).next().remove() // BR tag
$(this).remove()
})
0
 
LVL 4

Expert Comment

by:ramyajanarthanan
ID: 39599585
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/
0
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39600063
@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)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question