Solved

Remove Dynamic Input Box

Posted on 2013-10-24
3
415 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:N R
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 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 43

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This article discusses how to implement server side field validation and display customized error messages to the client.
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)

749 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