When a user starts typing in an empty input in a list I would like a new input to be appended to the list the for the next value to be added. Then, if they type in the new input, another new one is added and so on.
The code I have is..
$('.addNew').on('input', function(){
var $parentUl = $(this).closest('ul');
if($(this).val().length === 0){
$(this).removeClass('addNew');
$parentUl.append('<li class="catsLi"><input type="text" class="catItem addNew" name="reason[]" value=""/></li>');
}
});
This works but it adds a new input every time the users type another character rather than just on the first one. I have tried using one
instead of on
but then it will only add extra input, if the user types in the new input nothing happens.
With this in mind I thought I would check if the input is empty before appending a new input so it only does it once per input but it isn't working.
I can see from chrome inspect that when I type in the input, it isn't actually changing it's value which is why it always adds a new input so I have tried checking for $(this).text().length
instead but it still thinks it's empty.