6

var i = 1;
$('.button').click(function() {
  $('<br/><input name="name' + (++i) + '" type="text"/>').insertBefore(this);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
  <input name="name1" type="text" /><button class="button">+</button>
</div>

How can I place a button with [ + ] plus button, and when I click on it. button remove that field.

4
  • 1
    You want to remove the field on click of + button? Like how it is!! Atleast one button and textbox should be there right? Commented Aug 10, 2015 at 7:29
  • no I want there is also a - BUTTON when I click on it last field remove. Commented Aug 10, 2015 at 7:30
  • I had created this fiddle for some other solution. See if this helps link Commented Aug 10, 2015 at 7:32
  • @karan3112 I am new in JS I've tried alot but.... Commented Aug 10, 2015 at 7:34

4 Answers 4

7

Just add a new button template into your HTML string and a wrapping <div> to identify the row. Then use event delegation to bind onto the dynamically created remove buttons:

var i = 1;
$('.button').click(function() {
  // Wrap each row in a div. Won't need a <br> to create a line break and it's styleable
  $('<div><input name="name' + (++i) + '" type="text"/><button class="remove">-</button></div>').insertBefore(this);
});
// We need to use event delegation here, as the remove buttons are dynamically generated
$(document).on("click", ".remove", function() {
  // Remove the parent, which is the rows <div> element
  $(this).parent().remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="button">+</button>

Sign up to request clarification or add additional context in comments.

1 Comment

Good work but I don't want this. Thanks for response.
3

You can do something like this

var i = 1;
$('.button').click(function() {
  $('<br/><input name="name' + (++i) + '" type="text"/><button class="button1">-</button>').insertBefore(this);
});
$(document).on('click', '.button1', function() {
  $(this)
    .prev('input').remove()
    //removing input field
    .end().prev('br').remove()
    //removing br tag
    .end().remove();
  //removing the button
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
  <input name="name1" type="text" />
  <button class="button">+</button>
</div>

2 Comments

Exectly that I want, you again help me. Thanks alot.
@user3396968 : glad to help :)
2
var i = 1;
$('.button').click(function () {
    $('<input name="name' + (++i) + '" type="text"/><button class="remove">remove</button><br>').prependTo($(this).closest('div'));
});

$("div").on("click", ".remove",function () {
    $(this).prev("input").add($(this).next("br")).add($(this)).remove();
});

DEMO

Comments

1

I would highly recommend looking at using Knockout, Angular, or something like that. They're implementations of the MVVM pattern, which is quite useful in your scenario.

Here is a sample of how Knockout.js works, and directly correlates to your problem scenario.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.