0

I have this code that works fine, each time I click on "Add field" it will add new input field.

But I can't figure out how to implement the code to remove those fields if clicked on "Remove field".

Anyone that can help?

<a id="add-another" href="#">Add field</a>
<a id="remove-field" href="#">Remove field</a>

<script>
jQuery(document).ready(function ($) {
    var $field = $('.iphorm_1_3'); 
    $field.attr('name', $field.attr('name') + '[]');

    $('#add-another').click(function () {
        var $clone = $field.clone();
        $clone.val('').css({ display: 'block' }).appendTo($field.parent());
        return false;
    });

    $('#remove-field').click(function () {
      WHAT TO ADD HERE TO REMOVE THEM ONE BY ONE IF CLICKED?
    });


});

Here is example of the form on this test page: http://woodpharmacy.fusionidea.com/dddddddd/

3
  • So when you press remove-field you want to remove from bottom or from top? Commented Dec 20, 2016 at 17:00
  • I'd like from bottom Commented Dec 20, 2016 at 17:05
  • Gave you my answer Commented Dec 20, 2016 at 17:05

1 Answer 1

1

Use this. Since you have a variable $field you can find its siblings and select the last sibling using :last selector and remove it.

$('#remove-another').click(function (e) {
  $field.siblings(":last").remove();
  e.preventDefault();
});

Edit: Your webpage has remove-another as id but in question you have posted remove-field.

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

6 Comments

Removing and hiding are 2 completely different things. You might want to clarify that with OP.
@uom-pgregorio I am removing it right.. Thats what the OP wants
You had .hide() when I made my comment. Since you edited your answer already, just ignore it.
Now the complete code is this but for some reason it doesn't work:
No errors. This code is part of quform plugin, they give explanation how to add fields but not how to remove them, and i wanted to try. However it doesn't work with this code
|

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.