0

I was trying to use this:

Fiddle

But it seems that the jQuery version he is using is quite outdated.

However, the only necessary update I see in his code is to change .live() to .on(). But the remove button refused to work if I changed so.

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

Anyone has any idea why?

4
  • on() method has been added in jQuery 1.7, you're using the 1.4.3 version Commented May 12, 2013 at 2:06
  • That may also be because id must be unique! Commented May 12, 2013 at 2:06
  • @Sam I did notice that :) that code was not written by me. Commented May 12, 2013 at 3:06
  • @hjpotter92 I did try changing the ID to a class in my local test site, but it didnt work :( Commented May 12, 2013 at 3:07

2 Answers 2

1
  1. The ID must be unique (This is not causing the trouble though)
  2. Delegation is needed for newly added content.

jQuery

$(function () {
    var scntDiv = $('.p_scents');
    var i = $('.p_scents p').length + 1;

    $('#addScnt').on('click', function () {
        $('<p><label for="p_scnts"><input type="text" class="p_scnt" size="20" name="p_scnt_' + i + '" value="" placeholder="Input Value" /></label> <a href="#" class="remScnt">Remove</a></p>').appendTo(scntDiv);
        i++;
        return false;
    });

    $('.p_scents').on('click', '.remScnt', function () {
        if (i > 2) {
            $(this).parents('p').remove();
            i--;
        }
        return false;
    });
});

HTML

<h2><a href="#" id="addScnt">Add Another Input Box</a></h2>

<div class="p_scents">
    <p>
        <label for="p_scnts">
            <input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" />
        </label>
    </p>
</div>

This works perfectly: http://jsfiddle.net/uFkPx/

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

4 Comments

@vher2 if size() is deprecated, what's the new one? length()?
@HeHui .length is not a method; just a property. Therefore, $('.p_scents p').length and not $('.p_scents p').length() is used.
I'm assuming that would return how many p tags exists in .p_scents class?
@HeHui Yup. It will not be restricted to immediate children though!
1

Looks like it needs a rewrite, several deprecated methods, dynamic elements created from strings, targeting all parents(), same ID's etc :

$(function () {
    var scntDiv = $('#p_scents'),
        i       = $('#p_scents p').length + 1;

    $('#addScnt').on('click', function (e) {
        e.preventDefault();
        var p = $('<p />'),
            l = $('<label />', {for: 'inp_'+i}),
            j = $('<input />', {type: 'text', 
                                id: 'inp_'+i, 
                                size: 20,
                                name: 'p_scnt_' + i,
                                placeholder: 'Input Value'
                               }),
            a = $('<a />', {href : '#', 
                            id: 'remScnt_' + i,
                            'class' : 'remScnt',
                            text: 'Remove'
                           });

        p.append(l.append(j), a).appendTo(scntDiv);
        i++;
    });

    scntDiv.on('click', '.remScnt', function (e) {
        e.preventDefault();
        if (i > 2) {
            $(this).closest('p').remove();
            i--;
        }
    });
});

FIDDLE

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.