0

I have a form which processes data and puts it in to a csv. A user can add multiple rows to the form to add a new item. To make this a unique item the id is incremented by 1 (ex. item[0] goes to item1)

The form works great but I am trying to add a delete / remove button. It works except I don't want it to remove the very last item.

Below is the code I am using. I am having trouble as this if statement breaks the add crew button.

    $('#btnDelelte').click(function () {
        //remove the last form item
        $('.questions:last-child').remove();

        // if only element that remains as ID == 0, disable the "remove" button
        if ($('.questions:last-child').attr('id' == 0) 
            $('#btnDelelte').attr('disabled', 'disabled');

    });

If you comment out the if statement, and run the jsfiddle all the buttons work but you are able to delete the last form area.

jsfiddle

1
  • Please take the time to check if you have any syntax errors in your code before posting. Even in your jsfiddle link - the syntax error is right there in the console. Commented Jun 25, 2014 at 15:31

3 Answers 3

5

In the console it says:

Uncaught SyntaxError: Unexpected identifier

Looking at where the error is you can see the attribute method is not properly closed.

if ($('.questions:last-child').attr('id' == 0)  

It should be

if ($('.questions:last-child').attr('id') == 0) 
                                        ^
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response. I see the syntax error. I also not realize I was looking at it wrong as the ID [0] is a prefix on a longer ID. Thank you for your input.
0
 $('#btnDelelte').click(function () {           
       if($('.questions').length > 1){
           $('.questions:last-child').remove();
       }               
       });

Fiddle: http://jsfiddle.net/V2TAt/4/

1 Comment

Edit: What I should say is that if you add a new set of questions, delete it and then add another it does not incriment the idname[#]. It just keeps adding questions with the same idname[#].
0

Try this:-

Replace

if ($('.questions:last-child').attr('id' == 0) 
            $('#btnDelelte').attr('disabled', 'disabled');

with

if ($('.questions:last-child select').attr('id') == "town[0]")
           $('#btnDelelte').attr('disabled', 'disabled');

1 Comment

Edit: What I should say is that if you add a new set of questions, delete it and then add another it does not incriment the idname[#]. It just keeps adding questions with the same idname[#].

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.