Solved

Remove Dynamic Input Box

Posted on 2013-10-24
3
405 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
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 42

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Change properties of a dialog box 1 19
jQuery Scroll Speed 3 18
Re-position the objects 7 51
Need a good Angular tutorial 5 32
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now