cantaffordavan
cantaffordavan

Reputation: 1447

Jquery count the number of inputs on page

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?

Upvotes: 3

Views: 3419

Answers (1)

maček
maček

Reputation: 77826

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 

Upvotes: 5

Related Questions