1

Hello i have the following code jsfiddle. The add function works ok but the remove function doesn't work at all. Can you help me please solve the problem?

HTLM Part

Add Another Input Box

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

Jquery Part:

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

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

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

2 Answers 2

5

A problem with $('#remScnt').on('click', function() { is that you're trying to bind the event handler to the elements in the $('#remScnt') collection, which is empty at that time.

Another problem is that only one element can have a given id, so you must use a class if you want to be able to add more than one line.

So I recommend this construct :

    $('<p><label [...] class="remScnt"   [...]  ').appendTo(scntDiv);

    ...

    $('#p_scents').on('click', '.remScnt', function() { 

Demonstration

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

5 Comments

+1 although an explanation could be in place, and a context closer than document could easily be picked given the code in the question.
@DavidHedlund I fixed the context. I'll write a more detailled explanation.
@dystroy your code doesn't work. You need to bind to body. Your demonstration has correct code :)
@BasaratAli I just tested it with this selector, it works for me.
works for me as well. There was a disconnect between the demonstration and code sample. I see you've fixed it now. +1
1

You are binding to an element before it is created. You need to do live monitoring:

 $(document).on('click','#remScnt' ,function() { 

Working fiddle: http://jsfiddle.net/basarat/gnQzk/

That said, I recommend using a class instead of id.

ie. something like :

 $(document).on('click', '.remScnt', function() { 

3 Comments

It's illegal to give the same id to more than one element.
Yeah I was mentioning that :)
it's just some fast code ... i need to do .. for our secretary to work make some internal invoices that we use. and i knew it was something like that ( read the manual ) but i wasn't sure about the implementation of delegated events.

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.