3

I have a function to add and remove a field but the remove function doesnt work somehow.

HTML:

<div id="parts">
    Part
    <input type="text" id="auto_part" name="auto_part" />
                <br />
    Description
    <input type="text" id="auto_description" name="auto_description" />
                <br />
</div>
    <a href="#" id="addField">Add another part</a>

jQuery:

$(function() {
var scntDiv = $('#parts');
var i = $('#parts input').size();

$('#addField').on('click', function() {
    $('<br /><div id="parts"><span>Part</span> <input type="text" id="auto_part'+i+'" name="auto_part'+i+'" /><br />').appendTo(scntDiv);
    $('<span>Description</span> <input type="text" id="auto_description'+i+'" name="auto_description'+i+'" /> <br />').appendTo(scntDiv);
    $('<input type="hidden" id="row_count" name="row_count" value="" />').appendTo(scntDiv);
    $('<a href="#" id="removefield">Remove</a></div>').appendTo(scntDiv);
    i++;
    return false;
});

$('#removefield').on('click', function() { 
    if( i > 2 ) {
            $(this).parents('div').remove();
            i--;
    }
    return false;
});
});

The problem must have to do with this line:

$('#removefield').on('click', function() {

It doesnt pass that condition. When I click on Remove it doesnt do anything at all it just scrolls to the top.

nbrooks
  • 18,126
  • 5
  • 54
  • 66
Sinan Samet
  • 6,432
  • 12
  • 50
  • 93

2 Answers2

4

You are binding the click handler to the elements that are present in the DOM. But, your #removefield element is being dynamically added. So, the event handler is not attached to it.

You can use .on() to use event delegation and handle also future elements. Also, you may want to use classnames instead of and id attributes. id attributes need to be unique, but you can set the classname to as many elements as you want.

<a href="#" class="removefield">Remove</a>

$("#parts").on("click", ".removefield", function() { 
  /* ... */
});

The reason why your "Remove" link doesn't work is because you are adding the dynamic <div> element by parts hence making it invalid markup. You should be adding it all together at once. For example,

$('#addField').on('click', function () {
    var part = '<div id="parts' + i + '"><span>Part</span> <input type="text" id="auto_part' + i + '" name="auto_part' + i + '" /><br/>' +
        '<span>Description</span> <input type="text" id="auto_description' + i + '" name="auto_description' + i + '" /> <br />' +
        '<input type="hidden" id="row_count' + i + '" name="row_count' + i + '" value="" />' +
        '<a href="#" class="removefield' + i + '">Remove</a></div>';
    scntDiv.after(part);
    i++;
    return false;
});

$(document).on("click", ".removefield", function() {
    if( i > 2 ) {
        $(this).parent('div').remove();
        i--;
    }
    return false;
});

You can see it here.

Alexander
  • 23,432
  • 11
  • 63
  • 73
  • 1
    This works, but when I press remove now it removes all the parts divs $(this).parents('#parts').remove(); – Sinan Samet Feb 01 '13 at 23:01
  • @SinanSamet, that means your selector was wrong to start with. Can you fix your HTML layout? I don't think you are appending the elements where it seems you want them – Alexander Feb 01 '13 at 23:12
  • http://jquery-howto.blogspot.nl/2009/03/check-if-jqueryjs-is-loaded.html In that example it does work with the same name id and just removes the

    its in instead of all

    – Sinan Samet Feb 01 '13 at 23:25
  • @SinanSamet, I can't see how that link is relevant for what you asked or I said – Alexander Feb 01 '13 at 23:28
  • That does work but when it has a div around it, it removes that div aswell. In my case the container. http://jsfiddle.net/jQj23/2/ – Sinan Samet Feb 02 '13 at 13:50
  • 1
    @SinanSamet, you need to [use `.parent()` instead of `.parents()`](http://jsfiddle.net/YFwrM/) – Alexander Feb 02 '13 at 14:17
0

try

$('#removefield').live("click", function() {
Graham.Fraser
  • 369
  • 2
  • 12