3

I have a dynamic form that users can add/delete sets of input fields. I want to disable the delete form fields button when there is x amount of inputs left, so I thought I would count the number of inputs left on the onclick for the delete button, and then when there was only x amount of inputs left I would disable the delete button so they cant delete the last set of inputs.

$('#btnRemove').on('click', function() {
    $('.clonedInput').last().remove();
    if count of inputs = 7;
    $('#btnRemove').attr('disabled','disabled');
});

'if count of inputs = 7' is what I need the code for! Any help?

cantaffordavan
  • 1,427
  • 4
  • 24
  • 44
  • 1
    You should be using `prop` instead of `attr`: `$('#btnRemove').prop('disabled', true);` – Dave L. Jun 17 '12 at 19:06
  • @david Thanks for that. I am very new to jquery so I wasnt aware. Your comment led me to this post: http://stackoverflow.com/questions/5874652/prop-vs-attr which was very helpful – cantaffordavan Jun 17 '12 at 19:26

1 Answers1

5

I think you're just looking for

if ($('.clonedInput').length == 7) {
  // do stuff
}

Selectors simply return an array of jQuery elements. To access the length of an array in JavaScript, you simply use the .length property.

[1,2,3].length //=> 3 
maček
  • 76,434
  • 37
  • 167
  • 198
  • Ah! I was looking at the length function but I kept thinking it was counting the chars for some reason! Thank you very much this works just as I need. Accept in 6 mins – cantaffordavan Jun 17 '12 at 19:03