Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Remove Dynamic Input Box

Posted on 2013-10-24
3
Medium Priority
?
419 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
[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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

722 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